Javascript DOM HTML Element id Property set

Introduction

Change the id of an element:

document.getElementById("demo").id = "newid";

Click the button to change the value of the id attribute of the link.

View in separate window

<!DOCTYPE html>
<html>
<body>

<p><a id="myAnchor" href="https://www.java2s.com/">Examples</a></p>
<button onclick="myFunction()">Test</button>

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

<script>
function myFunction() {/* w w w  .  j ava  2  s  .  co  m*/
  document.getElementById("myAnchor").id = "newid";
  document.getElementById("demo").innerHTML = "The id of the link was changed from 'myAnchor' to 'newid'.";
}
</script>

</body>
</html>



PreviousNext

Related