Suppose I have the following HTML element:
<span id='kuku' class='lala bubu' value='xyz'>some text</span>
I know that .html()
returns the inner part of the element, i.e. some text
.
How could I get the whole element as string, containing <span>...</span>
?
To convert a HTMLElement to a string with JavaScript, we can use the outerHTML property. const element = document. getElementById("new-element-1"); const elementHtml = element.
The simplest way to do this is to create an element, insert the string into with innerHTML , then return the element. /** * Convert a template string into HTML DOM nodes * @param {String} str The template string * @return {Node} The template HTML */ var stringToHTML = function (str) { var dom = document.
Stringify a JavaScript ObjectUse the JavaScript function JSON.stringify() to convert it into a string. const myJSON = JSON.stringify(obj); The result will be a string following the JSON notation.
The Input Text Object in HTML DOM is used to represent the HTML <input> element with type=”text” attribute. The <input> element with type=”text” can be accessed by using getElementById() method.
Most browsers support the element.outerHTML
property. You may also want to check out the following Stack Overflow post for an alternative solution (for non IE browsers):
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With