I am trying to pass variable to a function with nested double quotes.
How do I escape the double quotes?
This is not working. Any ideas?
Any help would be appreciated.
It's kind of a strange problem. Triple nesting of quotes isn't possible, so you would think escaping the double quotes around Tom would work, but it doesn't. As far as I can tell, the rule is: you can only escape quotes that are the same as the next set of quotes surrounding that part of the string.
So, one solution is to start with single quotes:
onmouseover='change("My name is \"Tom\".")'
Or, strangely enough, what also works is using the html symbol for a quote: "
onclick="g('My name is "Tom".')"
but that is an ugly solution.
Question. Why do you have:
before the function name?
Thanks very much for your reply!
The problem with starting with single quotes is that now I will not be able to use a single quote in the string I am outputting. I have hardcoded this example because I assumed there was a simple solution.
This is also why using the html symbol for the quote is not really good either. I could write a function in php to change to the html symbols, but I was really looking for another solution.
Sorry to confuse you - that was a typo.
Thanks again for the help.
Try writing the data into the escape() function, which is like the php urlencode() function:
var text = escape(<?php echo $data; ?>);
Then, use unescape() when you want to display the text:
document.getElementById("someID").value = unescape(text);
This topic is now closed. New replies are no longer allowed.