Attribute getNamedItem() Method - Javascript DOM

Javascript examples for DOM:Attribute

Description

The getNamedItem() method returns the attribute node by name from a NamedNodeMap object.

Parameter Values

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

Return Value:

A Node object, representing the attribute node with the specified name

The following code shows how to Get the value of the onclick attribute of a button element:

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<body>

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

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

<script>
function myFunction() {/*from   w w w.  ja va  2  s.  com*/
    var a = document.getElementsByTagName("BUTTON")[0];
    var x = a.attributes.getNamedItem("onclick").value;
    document.getElementById("demo").innerHTML = x;
}
</script>

</body>
</html>

Related Tutorials