Avoid javascript from auto escaping and character

I see &in my text box, but only want &. I think this has something to do with how the browser interprets javascript .innerHTML. The text c &must go through the div. For some reason, I cannot directly assign a value to the text field.

HTML code

<div id='div'></div>
<input id='textbox' type='text' />

Javascript Code

var str = 'this & char';
$('#div').append("<div>" + str + "</div>");
$('#textbox').val($('#div').html());

Actual HTML output

<div>this &amp; char</div>

and the user sees this &amp; charin the text box.

Desire HTML code output

<div>this & char</div>

and the user sees this & charin the text box.

+4
source share
2 answers

.html(), - & &amp;. , , , &amp; , .

$('#textbox').val($('#div').text());
+2

html() text(). html() HTML, text() .

var str = 'this & char';
$('#div').append("<div>" + str + "</div>");
$('#textbox').val($('#div').text());
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id='div'></div>
<input id='textbox' type='text' />
+1

Source: https://habr.com/ru/post/1694089/


All Articles