JavaScript find li index within ul
Asked Answered
L

5

9

I'm trying to find the index of my list item by id in Javascript. For example I have list of 5 items and given an element I want to figure out which position it is in the list. Below is the code that I hope to build on.

It's using an onclick handler to find the element, which is working, I then just need to figure out the position of the element in the list 'squareList' in some way.

window.onload=function(){
    function getEventTarget(e){
        var e=e || window.event;
        return e.target || e.srcElement;
    }

    function selectFunction(e){
        var target=getEventTarget(e);
        alert(target.id);
    }

    var squareList=document.getElementById('squareList');
    squareList.onclick=function(e){
        selectFunction(e);
    }
}
Livialivid answered 18/8, 2013 at 4:21 Comment(4)
Please post your HTML markup along with this if you really want some help. Your best bet would probably be a jsFiddle. Is squareList id for the <ul>? Do your <li> elements have a shared class? Are there any nested lists? The answers to all these would be given with a jsFiddle.Northumbria
Would it not be acceptable to try something like thisDarwin
@Harry: value would make the HTML invalid. data-value, though...maybe.Snorkel
@cHao: Agreed, I just wanted to indicate a possible way (not the exact one). But correct, it could have been misleading :( I have updated it now.Darwin
O
20

To get the index, you can do:

Array.prototype.indexOf.call(squareList.childNodes, target)

And with jQuery, as you're already using cross-browser workarounds:

$(document).ready(function() {
    $('#squareList li').click(function() {
        var index = $(this).index();
    })
});
Omnibus answered 18/8, 2013 at 4:30 Comment(1)
simply the best!Brierroot
E
4

I have another solution and would like to share

function getEventTarget(e) {
  e = e || window.event;
  return e.target || e.srcElement; 
}

let ul = document.getElementById('squareList');
ul.onclick = function(event) {
  let target = getEventTarget(event);
  let li = target.closest('li'); // get reference by using closest
  let nodes = Array.from( li.closest('ul').children ); // get array
  let index = nodes.indexOf( li ); 
  alert(index);
};

You can verify here

Reference: closest

Edo answered 8/3, 2020 at 3:2 Comment(0)
C
4

With es6 and findIndex

Transform the ul node list into an array: [...UL_ELEMENT.children] then use findIndex to check for the element you are looking for.

const index = [...UL_ELEMENT.childNodes].findIndex(item => item === LI_ELEMENT)
Crone answered 8/4, 2020 at 23:24 Comment(0)
B
1

Convert the children of the ul element into an array using the spread operator [...] and then use the Array.prototype.indexOf() method passing the child element as the parameter.

const index = [...UL_ELEMENT.children].indexOf(LI_ELEMENT);
Biography answered 2/3, 2021 at 1:2 Comment(0)
H
-3

you can get all "li" in a list or array and then search the position with a simple loop

Horsa answered 18/8, 2013 at 4:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.