tags:

views:

121

answers:

6

I need to put this string in a variable:

chart cid="20"

but when I escape: \" this way:

AddSettings = AddSettings + "chart cid=\"0\"";

I still get a javascript error and the sentence shows up in the browser insted of get into the AddSetting variable.

I also tried this way:

AddSettings = AddSettings + 'chart cid="0"';

And the same thing happens.

+1  A: 

Can you post your whole javascript? or at least a little bit more? just to make sure there's nothing wrong around it?

Make sure the quotes are the regular quotes - not the special Microsoft Office quotes.

Paul
That’s rather a comment than an answer. So use the *add comment* link below the question.
Gumbo
I can't see an Add Comment link under the original post. I can see it under this answer of mine. But no-one else's.
Paul
You need some more reputation first. See the FAQ: http://stackoverflow.com/faq
Blixt
Paul doesn't have enough rep yet. You need 50 reputation points to leave comments.
Patrick McElhaney
A: 

It's hard to tell what the error is without knowing where you have this code. I'm assuming the code is in HTML, in which case you'll have to escape double quotes differently:

<button onclick="AddSettings += 'chart cid=&quot;20&quot;';">Click me!</button>

The reason for this is because HTML uses entities (&quot;) rather than escaping characters. The value for the onclick attribute will be converted to the following before being executed as JavaScript:

AddSettings += 'chart cid="20"'
Blixt
A: 

AddSettings = AddSettings + 'chart cid=\"0\"';

Max
That won't help. Personally, I'd say that just makes the code less readable
Blixt
A: 

If you're not using AddSettings before it gets to that line, you will have an error, as AddSettings has not been defined yet when you are adding it to itself. Make sure AddSettings has been defined prior to calling that line.

AlbertoPL
it is defined before that, tnks
A: 

Doublecheck if that is really what causes your problems. Both methods should work. From an interactive FireBug javascript session (which, BTW, is great to test such things):

>>> AddSettings = 'blabla';
"blabla"
>>> AddSettings += 'chart cid="20"';
"blablachart cid="20""

>>> AddSettings = "blabla";
"blabla"
>>> AddSettings += "chart cid=\"20\"";
"blablachart cid="20""
piquadrat
A: 

Guessing you are building an html tag? If that is the case html character entities are your friend

<input type="text" value="&#34;Hello Entities&#34;" />
epascarello