0

i have a dynamically created list with data of a json. So my question is how to get the index of a clicked item.

I tried this:

$("li").click(function() {
       var index = $("li").index(this);
       alert(index); });

But this didnt work. I dont get an alert? If i create some static Li elements, i get the index of them.

Please help me :/

4 Answers 4

2

Dynamically created lists will require delegated events...

$("ul").on("click", "li", function() {
       var index = $(this).index();
       alert(index);
});

If the <ul> is also dynamically created, please select a parent node that exists before the list is created.

Sign up to request clarification or add additional context in comments.

2 Comments

Works great :)) U help me a lot. Thanks
@user2561004 if you are satisfied with this answer, please mark it appropriately. Thanks.
1

Two issues:

  1. You just want $(this).index():

    $("li").click(function() {
       var index = $(this).index();
       alert(index); });
    

    From the documentation:

    If no argument is passed to the .index() method, the return value is an integer indicating the position of the first element within the jQuery object relative to its sibling elements.

  2. When you do $("li").click(...), it hooks it up to the elements that exist at that point in time. Ones you add later won't get the click handler assigned to them. The best way to approach dynamic lists like this is usually event delegation. DevlshOne gives you an example in his/her answer. The basic idea is this:

    $("selector for the list, not the list items").on("click", "li", function() {
        // handler code here
    });
    

    That hooks click on the list, not the list items, but then fires the handler when the click occurs as though you had hooked click on the handler.

2 Comments

His problem is directly related to the dynamic creation of the LI items, not the value of index.
His post contains the statement But this didnt work. I dont get an alert? If i create some static Li elements, i get the index of them.
0

Try the following. assuming that ul is not loaded dynamicaly.

$("ul.containerclass").on("click", "li", function() 
{
   var index = $(this).index();
   alert(index);
});

or

$(document).on("click", ".containerclass li", function() {
   var index = $(this).index();
   alert(index);
});

Note : here containerclass can be ID

Comments

-1

It is

$("li").click(function() {
       var index = $(this).index();
       alert(index);
});

3 Comments

His problem is directly related to the dynamic creation of the LI items, not the value of index.
How could anyone guess that? I do not recomend live events as the default solution to any problem and there is more than one way to add handlers to dynamically created lists.
His post contains the statement But this didnt work. I dont get an alert? If i create some static Li elements, i get the index of them. No guessing involved.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.