To get the tag name of an HTML element using javascript:
Let's assume we have an HTML element with the following markup:
<div id="example">text</div>
We can use the .tagName property to retrieve the tag name of this element, as shown below:
var element = document.getElementById("example");
var tagName = element.tagName;
// tagName = "div"
This code example is retrieving the tag name of an element by its ID. In this example, the element is identified by the ID "example", and the tagName variable will be set to the element's tag name, in this case, "div".
Let's assume we have an HTML element with the following markup:
<p class="paragraph">text</p>
We can use the .tagName property to retrieve the tag name of this element, as shown below:
var element = document.querySelector(".paragraph");
var tagName = element.tagName;
// tagName = "p"
0 Comments