1

In this fiddle : http://jsfiddle.net/pUeue/1449/

The alert does not fire correctly as there are quotes within the text :

<input type="button" value="test" />

$('input[type=button]').click( function() {
 s("test - <b>test</b> <b>est - test:  "test" for equities.");   
});

function s(value){
 alert(value)   
}

So if I use instead

<input type="button" value="test" />

$('input[type=button]').click( function() {
 s("test - <b>test</b> <b>est - test:  test for equities.");   
});

function s(value){
 alert(value)   
}

The quotes within the String in first example is causing the issue. Are there other characters I should be aware of which can cause similar issue ?

I think this question is related to List of all characters that should be escaped before put in to RegEx? but in my case I am referring to a javascript String instead of regex

Community
  • 1
  • 1
blue-sky
  • 51,962
  • 152
  • 427
  • 752
  • Note that the attribute value inside the selector sould be surrounded with quotes like `$('input[type="button"]')` instead of `$('input[type=button]')` – Stphane Apr 18 '14 at 09:55

8 Answers8

2

You can escape it using slash (other escape characters too - complete list of character that has to be escaped)

s("test - <b>test</b> <b>est - test:  \"test\" for equities.");   

Also you can have it within single quotes.

s('test - <b>test</b> <b>est - test:  "test" for equities.');   

Using double quotes within single quotes and vice versa doesn't need to be escaped.
Example:
1. " test 'test' "
2. ' test "test" '

Community
  • 1
  • 1
Praveen
  • 55,303
  • 33
  • 133
  • 164
  • 1
    @blue-sky ok I understood.. then it is [special character](http://msdn.microsoft.com/en-us/library/ie/2yfce773(v=vs.94).aspx).. the answer for your 2nd question is `slash`.. try like `console.log("\");\\nothing will be printed`, then escape it using `"\\"` ASIK that it. – Praveen Apr 18 '14 at 10:07
1

You can escape the quote by using the slash:

s("test - <b>test</b> <b>est - test:  \"test\" for equities.");

Or, use a single quote:

s("test - <b>test</b> <b>est - test:  'test' for equities.");

Or, this:

s('test - <b>test</b> <b>est - test:  "test" for equities.');

demo

Bhojendra Rauniyar
  • 83,432
  • 35
  • 168
  • 231
1

this should work:

s('test - <b>test</b> <b>est - test:  "test" for equities.');

Fiddle DEMO

Ehsan Sajjad
  • 61,834
  • 16
  • 105
  • 160
1

You simply miss the escape char "\".

Parser read the first " still the next thinking that the string is ended. Then read "test" and reports:

 Uncaught SyntaxError: Unexpected identifier 

So you must specify that your " is part of the string.

So:

s("test - <b>test</b> <b>est - test:  \"test\" for equities.");  
Luca Davanzo
  • 21,000
  • 15
  • 120
  • 146
0

replace outer double quotes with single quotes :

  $('input[type=button]').click( function() {
    s("test - <b>test</b> <b>est - test:  'test' for equities.");   
 });

Working Demo

Milind Anantwar
  • 81,290
  • 25
  • 94
  • 125
0

check fiddle http://jsfiddle.net/pUeue/1455/

$('input[type=button]').click( function() {
    test=20;
 s("test - <b>test</b> <b>est - test:  "+test+" for equities.");   
});

function s(value1){
 alert(value1)   
}

assuming test is variable

Pratik Joshi
  • 11,485
  • 7
  • 41
  • 73
0

Quotes should be escaped in javascript either by combination of single and double quotes or by using a \ to escape them.

Amit Joki
  • 58,320
  • 7
  • 77
  • 95
0

you should write in this way declare html code like

<input type="button" value="test" />

and write jQuery like

 $('input[type=button]').click( function() {
         alert($(this).val());
         var tmp = "test - <b>test</b> <b>est - test:  "+$(this).val()+" for equities.";
         s(tmp);   
        });

        function s(value){
         alert(value);  
        }

don't forget ; after alert(value);