Using strings containing single quotes as Javascript arguments within
inline HTML
Consider the following Javascript (which is probably not the best style):
parent.innerHTML = '<div onclick="foo(\'+string+\')">'+string+'</div>';
If string contains a single quote, the resulting HTML is not valid:
'<div onclick="foo('There's an error here')">There's an error here</div>'
Is it possible to deal with this issue using only a inline HTML solution?
In my case, the inline HTML is much larger, and I can't easily convert it
to a more 'proper' form, using DOM functions to create and append the
HTML, and closures for the onclick functions.
One possibility I've considered is using the element's own innerHTML as
the argument to foo() (since the function argument happens to be the same
as the content of the element), but simply using foo(this.innerHTML)
doesn't work, presumably because the innerHTML is not yet created.
No comments:
Post a Comment