sonill
sonill

Reputation: 568

find which li was clicked

suppose i have 3 li. what i want to do is when i click any li then i want to know which li was clicked and add an event according to it. how do i do it with jquery, any help or suggestions please

Upvotes: 0

Views: 571

Answers (2)

Sarfraz
Sarfraz

Reputation: 382656

jQuery will automatically capture the clicked element that you specify in the wrapped set:

$('ul li').click(function(){
  alert('I was clicked, my text is: ' + $(this).text());
});

See the example here.

You need to provide your html markup for exact what you need.

More Readings:

Upvotes: 2

thomasrutter
thomasrutter

Reputation: 117323

In jQuery, within an event handler the this keyword refers to the element which triggered the event.

$('li').click(function() {
  alert($(this).text()); // read out the text of the list item that was clicked
}

Upvotes: 3

Related Questions