Check which element has been clicked with jQuery Check which element has been clicked with jQuery jquery jquery

Check which element has been clicked with jQuery


Use this, I think I can get your idea.

Live demo: http://jsfiddle.net/oscarj24/h722g/1/

$('body').click(function(e) {    var target = $(e.target), article;    if (target.is('#news_gallery li .over')) {       article = $('#news-article .news-article');    } else if (target.is('#work_gallery li .over')) {       article = $('#work-article .work-article');    } else if (target.is('#search-item li')) {       article = $('#search-item .search-article');    }    if (article) {       // Do Something    }});​


So you are doing this a bit backwards. Typically you'd do something like this:

​<div class='article'>  Article 1</div><div class='article'>  Article 2</div><div class='article'>  Article 3</div>

And then in your jQuery:

$('.article').click(function(){    article = $(this).text(); //$(this) is what you clicked!    });​

When I see things like #search-item .search-article, #search-item .search-article, and #search-item .search-article I sense you are overspecifying your CSS which makes writing concise jQuery very difficult. This should be avoided if at all possible.


Answer from vpiTriumph lays out the details nicely.
Here's a small handy variation for when there are unique element ids for the data set you want to access:

$('.news-article').click(function(event){        var id = event.target.id;    console.log('id = ' + id); });