0

Given a dynamically built list such as :

<ul id="shortcuts">
  <li><input type="checkbox" value="false"/><button>foo</button><button>-</button></li>
  <li><input type="checkbox" value="false"/><button>foo</button><button>-</button></li>
  <!-- possibly many others lis -->
</ul>

So in order to use only one click event listener, I am trying to attach click event to the list and get the list item index that initially spread the event. Any of my attempts succeed so far.

I can get something out of this :

$("#shorcuts").click ( function(evt) {
   alert( evt.target );
});

But I can't get to the "li" html element nor its "index" within the list. Any idea what I am doing wrong ?

3 Answers 3

0

You need to attache event to li like below

$(function(){
  $('#shortcuts').on('click','li',function(){
    alert($(this).index());
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<ul id="shortcuts">
  <li><input type="checkbox" value="false"/><button>foo</button><button>-</button></li>
  <li><input type="checkbox" value="false"/><button>foo</button><button>-</button></li>
  <!-- possibly many others lis -->
</ul>

Here click event will delegate to all lis inside shortcuts and you can get the index of clicked li using .index()

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

Comments

0

$(document).ready(function(){
  $(document).on('click','#shortcuts li',function(e){
    e.preventDefault();
    alert($(this).text());
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul id="shortcuts">
  <li><input type="checkbox" value="false"/><button>foo1</button><button>-</button></li>
  <li><input type="checkbox" value="false"/><button>foo2</button><button>-</button></li>
  <!-- possibly many others lis -->
</ul>

2 Comments

Thanks for your answers. ….index() seems to always return 0. on(…) doesn't seem to throw the alert.
on() doesn't seem to throw the alert?
0

You can get index with JQuery - index function.

$('selector').click(function (event) {
    alert($(this).index());
});

DEMO

OR

$("#shorcuts").on("click","li", function(evt) {
    alert($(this).index());
}

Look at here for more options SO - Source

event-binding-on-dynamically-created-elements

bind-click-event-on-dynamically-created-elements

3 Comments

Looks like it's working if the list items are previously created. If they are later added, the event listener is not active for these new elements.
@Loic You have to attach click event dynamically when you are creating elements. I have added links to the answer. Have a look.
Thanks a lot Mike. Still can't get to work but the links are instructive.

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.