0

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)

2 Answers 2

1

HTML does not use Javascript-style escape strings.

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

Sign up to request clarification or add additional context in comments.

1 Comment

Then how can I convert it back to & in Javascript again? I cannot change the Div and I need output as &
0

Try this.

var other = eval( "'" + $("#D1").html() + "'" );

2 Comments

Thanks. It works. But I am not in favor of eval. I will select this if I don't get any other working answer.
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 evaling it otherwise.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.