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);
    }
}
Kemal Fadillah
  • 9,760
  • 3
  • 45
  • 63
Dan Rasmuson
  • 5,705
  • 5
  • 29
  • 45
  • 2
    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 `
      `? Do your `
    • ` elements have a shared class? Are there any nested lists? The answers to all these would be given with a jsFiddle.
    – DevlshOne Aug 18 '13 at 04:23
  • Would it not be acceptable to try something like [**this**](http://jsfiddle.net/hari_shanx/rshYq/) – Harry Aug 18 '13 at 04:25
  • @Harry: `value` would make the HTML invalid. `data-value`, though...maybe. – cHao Aug 18 '13 at 04:30
  • @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. – Harry Aug 18 '13 at 04:32

5 Answers5

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();
    })
});
Blender
  • 289,723
  • 53
  • 439
  • 496
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

Tuan Hoang
  • 586
  • 1
  • 7
  • 14
3

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)
ncubica
  • 8,169
  • 9
  • 54
  • 72
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);
Joan Gavelán
  • 91
  • 1
  • 3
-3

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

sheplu
  • 2,937
  • 3
  • 24
  • 21