views:

35

answers:

2

I have the following javascript

 var cst = "\x26";
 var other = $("#D1").html();

And my html has D1 div like the following

 <div id="D1">\x26</div>

Now when I add a breakpoint and see my two variables cst and other. One is shown & and another \x26. Also

cst.length == 1      

but

other.length == 4

I am confused. I need the value of other to be same (&) and I cannot change the Div. I am also using JQuery so I can use $(div_id)

+1  A: 

HTML does not use Javascript-style escape strings.

Your <div> actually says \x26, not &.

SLaks
Tanmoy
A: 

Try this.

var other = eval( "'" + $("#D1").html() + "'" );
Noel Walters
Thanks. It works. But I am not in favor of eval. I will select this if I don't get any other working answer.
Tanmoy
Use the text content (`text()` in jQuery) not `innerHTML`/`html()` which will have extra HTML-encoding applied. `JSON.parse` is safer then `eval` where available. You may want to check that there's no unescaped quote or newline in the string before `eval`ing it otherwise.
bobince