2
        var originalHtmlString = '<p>Some text</p>'

        //Create an html object called myHtml that I can work with like w/ document
        var myHtml = document.createElement( 'html' );

        //Make the html object hold the html I wrote in a string so I can edit it as html
        myHtml.innerHTML = originalHtmlString;

        var stringifiedHtml = ???

How do I turn the "myHtml" var back into a string after working with it?

0

1 Answer 1

3

Should be as easy as

var stringifiedHtml = myHtml.outerHTML;

Element.outerHTML

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

6 Comments

Thanks, I will come back to accept, sorry for dumb question.
@VSO That wasn't a dumb question, it never occurred to me that was even possible.
@zer00ne Good to hear, there's so much stuff out there in terms of html selectors and things like this that most people (at least that I know) dont ever use, especially when heavily depending on angular. Thanks again ade.
You're welcome, the question was fine, and when you know about outerHTML it just seems obvious that it does the opposite of innerHTML, but it's not something that is used very often, and lots of people apparently doesn't even know it exists and have been supported for decades.
Does outerHTML really do the "opposite" of innerHTML though? From what was given in OP's question, doing var stringifiedHtml = myHtml.innerHTML would have been an equally good answer.. the difference between them is whether the element itself is included or not in the returned string. Both properties can be written or read.
|

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.