+ 1
How to get particular index by click event in js
I am confused to get index by click event <ul class="list"> <li>a</li> <li>b</li> </ul> If we get document.getElementsByClassName("list"); It will return an array so I want to get a particular index of an element like first tag li tag index is 0 2nd li tag index is 1
5 ответов
+ 3
Is this what you mean?
https://code.sololearn.com/WpmE6o6X0HOU/?ref=app
+ 1
What does index mean here? did you mean to get the text in one <li> element one at a time?
Which element should trigger this on click event?
+ 1
document.getElementsByClassName("list"); will return a collection of all element references having "list" as class name, or undefined in case no element was found to match the class name filter.
I see in the code <ul class="list">. So document.getElementsByClassName("list") will (on success) return a collection of <ul> element references, rather than a collection of <li> elements.
Anyways, which element is to be clicked to trigger the behaviour? a <button>? or something else?
+ 1
Yes
0
When we click on any li tag it should return their appropriate index