Reputation: 568
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
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());
});
You need to provide your html markup for exact what you need.
More Readings:
Upvotes: 2
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