Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert an HTML element to a string, including the opening and closing tags?

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>?

like image 909
Misha Moroshko Avatar asked Sep 10 '10 04:09

Misha Moroshko


People also ask

How do I turn a element into a string?

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.

How do you convert a string in HTML?

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.

How do you turn an object into a string in JavaScript?

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.

What does object Htmlinputelement mean?

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.


1 Answers

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):

  • How do I do OuterHTML in firefox?
like image 89
Daniel Vassallo Avatar answered Oct 07 '22 01:10

Daniel Vassallo