Forum Moderators: open
I remember a thread on this in this forum, but as search is inactive I'll ask again, since I am running into a similar Javascript problem. Consider:
<div onclick="foo('myLink');">division</div>
In Javascript this would be written as:
strMyLink = "<a href=\"http://www.mysite.com\">";
str += "<div onclick=\"foo(" + strMyLink + ");\">division</div>";
The problem here is of course that I have to escape the quotes around "http://www.mysite.com" with quotes other than " or ', or else the function foo ends inappropriately.
How does one fix this problem in general?
Thanks in advance
Tech
strMyLink = "<a href=\"http://www.mysite.com\">";
str += "<div onclick=\"foo(" + strMyLink + ");\">division</div>";
1) str hasn't been initialised.
2) Is the string for the <a> tag (held in strMyLink) really meant to go inside the foo() call?
-----------
Generally, it's easier if you start the nesting with single quotes.
strMyLink = '<a href="http://www.mysite.com">';
With your complicated one above, it's probably easier to build the string, as you have been doing. This may do it though:
'<div onclick="foo(\\\'<a href=\\\"http://www.mysite.com\\\">\\\');">division</div>'
document.write('<div onclick="foo(\'myLink\';">division</div>');
It is entirely possible that you may need to break up the "div" start and end tags.
document.write('<di'+'v onclick="foo(\'myLink\';">division</d'+'iv>');