Javascript DOM HTML NamedNodeMap item() Method get second attribute

Introduction

Change the value of a <button> element's second attribute:

document.getElementsByTagName("BUTTON")[0].attributes[1].value = "newClass";

Click the button to change the value of the button element's second attribute.

View in separate window

<!DOCTYPE html>
<html>
<head>
<style>
.example {//from w w w  .j a  v a2s  . co  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>



PreviousNext

Related