How do i get the href value with js with the ClassName selector? | 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 value with js with the ClassName selector?

$('.sessionBtn').on('click', function (e) { e.preventDefault(); var url_string = document.getElementsByClassName("sessionBtn")[0].parentNode.parentNode.href; document.getElementById("demo").innerHTML = "document id: "+url_string; This throws an error of undefined. Error: document id: undefined

17th Jun 2018, 9:16 PM
Peter Claver
Peter Claver - avatar
2 Answers
+ 3
It's because you are trying to access to a function before the document is loaded I would recommend to set your whole js into an onload = () => {...} function. And it would be cool if you will send a link to your code and don't paste the whole code here because the responders will be able to test the code directly :)
17th Jun 2018, 9:20 PM
Michael55555
Michael55555 - avatar
+ 2
And I also wod like to remove and you that if you tick the chop on the left side of an answer you can mark it as best it costs you anything and supports the responder with 15px it also helps to find the correct answer much faster for other programmers :)
17th Jun 2018, 9:24 PM
Michael55555
Michael55555 - avatar