moey
moey

Reputation: 10897

.html() to Not Encode the Passed Value

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 &lt;. 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

Answers (3)

gdoron
gdoron

Reputation: 150293

text won't encode the value:

$('#my-title').html('>');
alert($('#my-title').text() + ' not encoded');
alert($('#my-title').html() + ' encoded');
​

JSFiddle DEMO

Upvotes: 0

JJJ
JJJ

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

Rory McCrossan
Rory McCrossan

Reputation: 337691

Use text() instead of html()

var v = $('#my-title').text();

Upvotes: 2

Related Questions