Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Change <a> href with same class on JavaScript

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…

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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");
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading