I want to change (or control…) my <a> href with JavaScript
<a class="google" href="#">LINK ONE</a>
<a class="google" href="#">LINK TWO</a>
And a JavaScript :
document.querySelectorAll("a.google").href = "https://google.com"
If I use querySelector(".google") it change the link, but just one of theme. If I use querySelectorAll(".google") or ("a.google") it doesn’t change anything…
Can anyone help me?
>Solution :
document.querySelectorAll returns an array of elements. For this to work, and it was a good idea to think about it, you can do like so :
const links = document.querySelectorAll("a.google");
links.forEach(link => link.href = "https://google.com");