Element id Property - Change the id of an element: - Javascript DOM

Javascript examples for DOM:Element id

Description

Element id Property - Change the id of an element:

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<body>

<p><a id="myAnchor" href="https://www.java2s.com/">java2s.com</a></p>

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

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

<script>
function myFunction() {/* www. j  av  a 2  s  .com*/
    document.getElementById("myAnchor").id = "newid";
    document.getElementById("demo").innerHTML = "id changed from 'myAnchor' to 'newid'.";
}
</script>

</body>
</html>

Related Tutorials