Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I've got an XML document that contains a tag that has well-formed HTML content. I need to get that HTML into my page using JavaScript. However, due to CMS issues, the HTML CANNOT be escaped with < ![CDATA[ ]]> or anything else, and the <> must be present, not encoded to &lt ; &gt ;

<submenu>
    <content>
        <div>
            <h3>Hello World</h3>
            <p>Lorem <a href="ipsum.html">ipsum</a></p>
        </div>
    </content>
</submenu>

I've used jQuery to get the XML and put the submenu's into an array. I'm able to get the text with:

$(menuArray[n]).find('content').text();

However, this just returns "Hello World Lorem ipsum". I need the HTML. Unfortunately jQuerys' .html() method doesn't work with XML.

Is there any other way? Thanks in advance.

share|improve this question
it works for me using browser's xml parser. – kjy112 Feb 12 '11 at 21:56
add comment (requires an account with 50 reputation)

2 Answers

up vote 6 down vote accepted

Not overly clean but could you not use something like found in this example JQuery Object to Sring and do something like... var myHTML = $('<div>').append($(menuArray[n]).find('content').clone()).remove().html();

Ugly I know but should work

share|improve this answer
It does indeed. Thank you. – thugsb Feb 12 '11 at 22:30
This will thrown SCRIPT5022: DOM Exception: HIERARCHY_REQUEST_ERR (3) on IE9 – Diego ZoracKy Dec 3 '12 at 19:45
I Solved the problem told by me in the last comment. By setting dataType to "html" – Diego ZoracKy Dec 3 '12 at 20:11
add comment (requires an account with 50 reputation)

@Sam Nicholson gave us a good idea. I used it for a while, but now i solved my problem through another way.

By setting AJAX property 'dataType' to 'html' and forcing the server response to be text/html rather than text/xml. jQuery will let you use .html() to manipulate the nodes of your XML tree sent from your server

share|improve this answer
add comment (requires an account with 50 reputation)

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.