0

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.

3
  • the 2nd way should work, are you sure you reloaded your javascript when you implemented it? Commented Aug 10, 2009 at 13:26
  • Do you print that code using another language? Commented Aug 10, 2009 at 13:27
  • 2
    what javascript error do you get? I can't see any problem with these snippets. Commented Aug 10, 2009 at 13:28

6 Answers 6

1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

That’s rather a comment than an answer. So use the add comment link below the question.
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 doesn't have enough rep yet. You need 50 reputation points to leave comments.
0

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"'

Comments

0

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

1 Comment

That won't help. Personally, I'd say that just makes the code less readable
0

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.

1 Comment

it is defined before that, tnks
0

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""

Comments

0

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;" />

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.