4

I am parsing some data from feedburner of which contains HTML entities. I am trying to encode the HTML entities using jQuery as such:

var encodedStr = data['1']['result']['content']; // content with HTML entities
$("#content").html(encodedStr).text();

but with no results.

Here is what its parsed: http://jsbin.com/ihadam/1/edit

7
  • 1
    What exactly are you trying to do? This is unclear. Commented Mar 30, 2013 at 0:28
  • Do you want to interpret the <p> stuff as actual HTML tags when you insert it? Commented Mar 30, 2013 at 0:29
  • @Barmar Yes. Check this: jsbin.com/ihadam/2/edit - I want it to be an actual image tag rather than a string. Commented Mar 30, 2013 at 0:30
  • I think this might do what you need: stackoverflow.com/questions/1219860/… stackoverflow.com/questions/10715801/… Commented Mar 30, 2013 at 0:34
  • @TravisSharp It's exactly what I said I have tried of which doesn't work. Commented Mar 30, 2013 at 0:36

2 Answers 2

2

Basically you should encode your html entities into html as such:

var encodedStr = data['1']['result']['content'];
var a = $("#content").html(encodedStr).text();

Then get the encoded text and apply it as html() as such:

$("#content").html(a);

That should work.

Demo: http://jsbin.com/ihadam/9/edit

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

Comments

0

You can save the bloat of JQuery with pure JavaScript functions.

Sometimes you just want to encode every character... This function replaces "everything except for nothing" in regxp.

function encode(e){return e.replace(/[^]/g,function(e){return"&#"+e.charCodeAt(0)+";"})}

function encode(w) {
  return w.replace(/[^]/g, function(w) {
    return "&#" + w.charCodeAt(0) + ";";
  });
}

test.value=encode(document.body.innerHTML.trim());
<textarea id=test rows=11 cols=55>www.WHAK.com</textarea>

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.