0

I need to know how to change the url of a link.

For example www.sitename.com to www.anothersitename.com

How do i do this using javascript?

<a id="myAnchor" href="http://www.microsoft.com">Microsoft</a>
<button onclick="myFunction()">Change link</button>

function myFunction() {
//For example How to change Microsoft url to w3 schools url?
//what do i put here?
}
1
  • 1
    function myFunction() { document.getElementById("#myAnchor").href ="" } Commented Jul 7, 2019 at 22:55

1 Answer 1

4
function changeHref() {
  document.getElementById("myAnchor").setAttribute("href", "www.google.com")
}

this will set the href attribute of your link to google.com. Of course this URL must not be hardcoded and can be replaced with a function parameter.

Sign up to request clarification or add additional context in comments.

Comments