Question

I have an input box which is populated with a string from an apex method. If I use an apostrophe in the String, I receive an 'Unexpected Identifier' error.

Heres the code:

            $('#textInput').append(
                $('<input>').addClass('ui-input-note').attr('placeholder', 'Notes')
                    .val('{! CurrentNote  }')
            );

This is what displays in the console:

            $('#textInput').append(
                $('<input>').addClass('ui-input-note').attr('placeholder', 'Notes')
                    .val('Jim's dog')
            );

I'm assuming the apostrophe is exiting the string. How do I get around this?

Other than using " ".

Cheers

Was it helpful?

Solution

You can do it as follow:

  1. catch the escape character on its key in event

     $(document).keypress(function(event){
           alert(String.fromCharCode(event.which)); 
      })
    
  2. append the \ with escape characters to the user input.

     //Append text to input element
      function jQ_append(id_of_input, text){
          if(text=='`'){
               text='\`';
           }
           $(id_of_input).val($(id_of_input).val() + text);
       }
    

OTHER TIPS

You can use the escape() (and then the unescape() ) function.

Here more info and examples: JavaScript escape() Function

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top