Javascript Reference - HTML DOM getNamedItem() Method








The getNamedItem() method returns the attribute node with the specified name from a namedNodeMap.

Browser Support

getNamedItem Yes Yes Yes Yes Yes

Syntax

namedNodeMap.getNamedItem(name);

Parameters

Parameter Type Description
nodename String Required. The name of the node in the namedNodeMap to return




Return Value

It returns a Node object for the specified name.

Example

The following code shows how to get the value of the onclick attribute of a button element.


<!DOCTYPE html>
<html>
<body>
<button onclick="myFunction()">test</button>
<!--from   ww w .j  a  v  a  2  s  .com-->
<p id="demo"></p>

<script>
function myFunction() {
    var a = document.getElementsByTagName("BUTTON")[0];
    var x = document.getElementById("demo");  
    x.innerHTML = a.attributes.getNamedItem("onclick").textContent;
}
</script>
</body>
</html>

The code above is rendered as follows: