Javascript DOM HTML Element setAttributeNode() Method set href attribute of <a>

Introduction

Set the href attribute node of a <a> element:

Click the button to create a "class" attribute with the value "www.java2s.com" and insert it to the a element above.

View in separate window

<!DOCTYPE html>
<html>
<body>

<a id="myAnchor">A Link: Go to java2s.com</a>
<button onclick="myFunction()">Test</button>

<script>
function myFunction() {//from  w w w .  ja  v  a2  s.  c  om
  var anchor = document.getElementById("myAnchor");
  var att = document.createAttribute("href");
  att.value = "https://www.java2s.com";
  anchor.setAttributeNode(att);
}
</script>

</body>
</html>



PreviousNext

Related