Getting the class of the element that fired an event using JQuery

Redbox picture Redbox · Jun 14, 2012 · Viewed 186.9k times · Source

is there anyway to get the class when click event is fired. My code as below, it only work for id but not class.

jsfiddle code here

Answer

broesch picture broesch · Jun 14, 2012

Try:

$(document).ready(function() {
    $("a").click(function(event) {
       alert(event.target.id+" and "+$(event.target).attr('class'));
    });
});