Say I've got this HTML page:
<html>
<head>
<script type="text/javascript">
function echoValue(){
var e = document.getElementById("/path/$whatever");
if(e) {
alert(e.innerHTML);
}
else {
alert("not found\n");
}
}
</script>
</head>
<body>
<p id="/path/$whatever">The Value</p>
<button onclick="echoValue()">Tell me</button>
</body>
</html>
I would assume that the browser treats the ID-string /path/$whatever
as simple string. Actually, it converts the $
to it's rendered representation ($
).
The javascript code however uses the literal string $
to search for the element. So, the call document.getElementById
fails and I never get hands on the value of the paragraph.
Is there a way to force the browser into using the given ID string literally?
Edit:
Of course I know that I don't have to escape the $
. But the web page gets generated and the generator does the escaping. So, I have to cope with what I've got.
In the
<p id="...">
, the$
sequence is interpreted as$
, because it appears in an attribute and is treated as an HTML entity. Same goes for all other element attributes.In the
<script>
element, HTML entities are not interpreted at all, so it shows up literally.I'd suggest you to decode the HTML entity in your javascript code:
You could try decoding the javascript text without jQuery:
JSFiddle: http://jsfiddle.net/phTkC/