How do I get the href of an anchor with JS? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

How do I get the href of an anchor with JS?

I'm making a scroll animation. But I've made it with jQuery (it seems easier), but I don't know how do it with pure JS. In case, I have a menu list, and I get the href anchor to move the page up and down.

19th Apr 2018, 2:12 AM
Thiago Martins Saraiva
Thiago Martins Saraiva - avatar
1 Answer
+ 6
<a id="anchor" href="src/a.html"> document.getElementById("anchor").getAttribute("href"); // Gets actual string 'src/a.html'. document.getElementById("anchor").href; // Gets full path 'http://mydomain.com/src/a.html'.
19th Apr 2018, 2:24 AM
John Wells
John Wells - avatar