Javascript DOM HTML Element tagName Property

Introduction

The tagName property returns the tag name of the element.

Get the tagName of an element:

var x = document.getElementById("myP").tagName;

View in separate window

<!DOCTYPE html>
<html>
<body>

<p id="myP">Click the button to display the tag name of this element.</p>

<button onclick="myFunction()">Test</button>

<p id="demo"></p>

<script>
function myFunction() {/* w w  w.  j  a v  a  2  s  . c  o  m*/
  var x = document.getElementById("myP").tagName;
  document.getElementById("demo").innerHTML = x;
}
</script>

</body>
</html>

In HTML, the returned value of the tagName property is in Uppercase.

This property is read-only.

You can also use the nodeName property to return the tag name of an element.

The nodeName returns attribute nodes, text nodes, comment nodes.




PreviousNext

Related