0

I am looking for a Element property (something like innerHTML or outerHTML) to get the String representation of the Tag

I know it's hard to explain in my question title, but this is an example.

<div id="div1" align="center" style="border:1px solid red">
 This is some text in a div element!
</div>

Expected output is

<div id="div1" align="center" style="border:1px solid red">

If such thing is not available then, best way in my mind is to do this by using outerHTML + regex.

Tharaka Deshan
  • 1,349
  • 3
  • 15
  • 30

1 Answers1

-1

You can try this quick solution to fetch the parent tag :-

var element = document.getElementById("div1")
var formatedTag = element.outerHTML.replace(element.innerHTML, "");

console.log(formatedTag); // result :- "<div id="div1" align="center" style="border:1px solid red"></div>"

Hope this should help!

Jay
  • 338
  • 1
  • 5