1

This JS code tries to modify the raw html. It does that by converting the html to jQuery element, does the modifications on the jQuery element then the part which is not working is converting back to raw html string.

Since .html() will not work with xml as indicated in the docs
How can it convert the jQuery back to raw html string? Thanks

let jQ = $($.parseHTML(raw_html));
//modify jQ to heart content
console.log(jQ.html()); //<-- undefined

The raw_html

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
//...

</html>

edit
Output of console.log($.parseHTML(raw_html));
enter image description here

3
  • html() works with xml. JQuery said so only because to play safe. Commented Aug 19, 2016 at 1:18
  • What does $.parseHTML(raw-html) return? Is that defined, note that raw-html isn't a proper variable name because of the subtraction. Commented Aug 19, 2016 at 1:18
  • @SpencerWieczorek console.log($.parseHTML(raw_html));returns an object, I am posting its image now Commented Aug 19, 2016 at 1:26

1 Answer 1

1

Do var a = $('<div>').append(raw_html);

//do modifications to variable a

$(a).html() will display the correct html

NOTE this will strip head and other tags as discussed here

heres a plnkr

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.