+ 1

how getElementByTagName retuns value for links?

suppose I have 500 lines of code in html and many links are there... And I want to change Nth link using JS, so do I have to count the value of N.... because the getElementByTagName would return an array of all the links ?

14th Aug 2018, 1:31 AM
Gaurav Zaiswal
Gaurav Zaiswal - avatar
4 odpowiedzi
+ 2
Well in that case if you know which element is (nth), just specify it like an array. If not, loop through them all checking if the href is the one you are looking for. I strongly recommend you using an if on that tag to make it easier. And also to check out the document.querySelector
14th Aug 2018, 2:06 AM
voidneo
+ 2
Of course you need to give the count to program, otherwise how does program know which link to access? Or just assign an id to the selected link, use getElementById to access it.
14th Aug 2018, 2:34 AM
Calviղ
Calviղ - avatar
+ 1
the getElementById method is supposed to return only one element, because the id is not meant to be repeated to avoid this particular problem. But if it does return an array as you say, you'll have to specify which one
14th Aug 2018, 1:55 AM
voidneo
+ 1
noname sorry, I wanted to ask about getElementByTagName ... when tag is <a> . P.S. I have edited the question.
14th Aug 2018, 1:59 AM
Gaurav Zaiswal
Gaurav Zaiswal - avatar