1

Possible Duplicate:
how to make iphone apps like ibeer, imilk, ibug, ibeer
Use javascript to get raw html code

Let's say i have an html section like this:

<div id="post_content">
  <span>&#9654;<span>
</div>

(this is right pointing triangle)

I want to get the child of the "post_content" as raw html, that is as a string that is exactly the same as in the code above, so:

"<span>&#9654;<span>"

When I do

$('#post_content').html() 

the content of the span get's transformed to something else it seems, when I post it using $.post() for example it turns into %E2%96%B6.

How do I get the content of post_content as a string that is exactly the same the original?

Community
  • 1
  • 1
Hoff
  • 38,776
  • 17
  • 74
  • 99
  • related: [rules regarding special characters/entities and innerHTML](http://stackoverflow.com/a/712081/1048572) – Bergi Sep 14 '12 at 12:36
  • 2
    check this answer [You cannot get the actual HTML source of part of your web page][1] [1]: http://stackoverflow.com/a/3905503/885152 – secondflying Sep 14 '12 at 12:50
  • the above comment is corrent, i.e. it is not easily possible - second flying, if you post this your comment as an answer, I'll mark it as accepted. – Hoff Sep 14 '12 at 14:23

2 Answers2

0

Take a look here :

How to decode HTML entities using jQuery?

Basically using :

var decoded = $("<div/>").html(encodedStr).text();

Will get you what you want.

Community
  • 1
  • 1
Yahel
  • 8,522
  • 2
  • 24
  • 32
  • The OP wants to do it rather the other way round – Bergi Sep 14 '12 at 13:10
  • That's not what I understand : When he posts the text as a variable, the post mecanism converts the entity again. So my answer provides a way to get back to the previous version. – Yahel Sep 14 '12 at 14:14
0

You can't, as explained in this excellent answer the received HTML is parsed into a DOM, only which is available to scripting. You only could get the plain text from the responseText of an XMLHttpRequest.

If you get the innerHTML from the DOM (what $el.html() does), it is serialized. HTML5 specifies how to do that, but legacy browsers are most often not compliant.

You issue with $.post() is that this method sends the text in a HTTP request - encoded of course. What you see is just encodeURIComponent applied on the Unicode string you got. Try to alert() or console.log() the value, and you will see its true representation.

Community
  • 1
  • 1
Bergi
  • 630,263
  • 148
  • 957
  • 1,375