Home >Web Front-end >JS Tutorial >How to use click event index in js
The property in JavaScript that gets the position of the clicked element in the parent element is element.index. It is used to determine the position of a clicked element in a list or collection, such as setting a click event on a li list item and getting its index to determine the clicked item.
Usage of click event index
in JavaScript
Click eventindex
Attribute indicates the position of the triggered element in the parent element. In JavaScript, this property can be accessed via:
<code class="javascript">element.index</code>
Usage:
index
The property is typically used to determine which element was clicked A position in a related list or collection. For example, the following code gets the index of a list item when it is clicked:
<code class="javascript">const listItems = document.querySelectorAll('li'); for (let i = 0; i < listItems.length; i++) { listItems[i].addEventListener('click', function() { console.log(this.index); }); }</code>
Advantages:
Disadvantages:
ol
) or div
Available when using other elements. Other notes:
index
The attribute does not work for full page click events. index
property will be updated accordingly. index
property may return incorrect values. The above is the detailed content of How to use click event index in js. For more information, please follow other related articles on the PHP Chinese website!