Attribute item() Method - Javascript DOM

Javascript examples for DOM:Attribute

Description

The item() method returns the node at the specified index in a NamedNodeMap, as a Node object.

Parameter Values

Parameter Type Description
index Number Required. The index of the node in the NamedNodeMap

Return Value:

A Node object, representing the attribute node at the specified index.

The following code shows how to Get the name of the first attribute of a <button> element:

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<head>
<style>
.example {/*from  ww  w .jav a 2s . c o  m*/
    color: red;
    padding: 5px;
    width: 150px;
    font-size: 15px;
}

.newClass {
    color: blue;
    padding: 15px;
    width: 250px;
    font-size: 18px;
    background-color: white;
}
</style>
</head>
<body>

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

<script>
function myFunction() {
    var a = document.getElementsByTagName("BUTTON")[0];
    a.attributes[1].value = "newClass";
}
</script>

</body>
</html>

Related Tutorials