Document createAttribute() Method - Create a href attribute, with the value "www.java2s.com", and insert it to an <a> element: - Javascript DOM

Javascript examples for DOM:Document createAttribute

Description

Document createAttribute() Method - Create a href attribute, with the value "www.java2s.com", and insert it to an <a> element:

Demo Code

ResultView the demo 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() {/* www  . j  a va  2s  .c  o m*/
    var anchor = document.getElementById("myAnchor");
    var att = document.createAttribute("href");
    att.value = "http://www.java2s.com";
    anchor.setAttributeNode(att);
}
</script>

</body>
</html>

Related Tutorials