Reputation: 10897
I need to keep each character passed to .html()
as is i.e. not encoded. For example, .html('>')
to save the less-than character as >
, not <
. How would I do this? Ideally, the solution would apply to all "special" characters e.g. <
, &
, etc.
<h1 id="my-title"></h1>
// JavaScript / jQuery code.
$('#my-title').html('>');
...
...
// Somewhere else in the code, I need to retrieve the value back by calling
// var v = $('#my-title').html();
Upvotes: 1
Views: 1276
Reputation: 150293
text
won't encode the value:
$('#my-title').html('>');
alert($('#my-title').text() + ' not encoded');
alert($('#my-title').html() + ' encoded');
Upvotes: 0
Reputation: 33153
Instead of .html()
use .text()
to save and retrieve the value.
$('#my-title').text('>');
var v = $('#my-title').text();
alert( v ); // alerts >
Note that you need to use .text()
for saving the value as well. There's a reason why <
and >
become encoded...
Upvotes: 1
Reputation: 337691
Use text()
instead of html()
var v = $('#my-title').text();
Upvotes: 2