<!DOCTYPE html>
<html>
<body>

<a id="myAnchor">A Link: Go to w3schools.com</a>

<p>Click the button to set the href attribute with a value of "www.w3ii.com" of the a element above.</p>

<button onclick="myFunction()">Try it</button>

<p><strong>Note:</strong> Internet Explorer 8 and earlier does not support the setAttribute method.</p>

<script>
function myFunction() {
    document.getElementById("myAnchor").setAttribute("href", "http://www.w3ii.com");
}
</script>

</body>
</html>