The getNamedItem()
method returns the attribute
node with the specified name from a namedNodeMap.
getNamedItem |
Yes | Yes | Yes | Yes | Yes |
namedNodeMap.getNamedItem(name);
Parameter | Type | Description |
---|---|---|
nodename | String | Required. The name of the node in the namedNodeMap to return |
It returns a Node object for the specified name.
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: