Element setAttribute() Method - Add a href attribute with a value of "java2s.com" to an <a> element: - Javascript DOM

Javascript examples for DOM:Element setAttribute

Description

Element setAttribute() Method - Add a href attribute with a value of "java2s.com" 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() {//from ww w. j  av a 2  s  . co m
    document.getElementById("myAnchor").setAttribute("href", "http://www.java2s.com");
}
</script>

</body>
</html>

Related Tutorials