1

Im able to get id, src, style or whatever with:

$(document).click(function (e) {
       console.log('id :' + e.target.id);
});

But I want to get the class, why isnt that possible?

FishBasketGordo
  • 22,904
  • 4
  • 58
  • 91
WIRN
  • 915
  • 1
  • 16
  • 31
  • possible duplicate of [Get Class List for Element with jQuery](http://stackoverflow.com/questions/1227286/get-class-list-for-element-with-jquery) -- please [use the search](http://stackoverflow.com/search?q=jquery+get+element+class) before you ask a new question. – Felix Kling May 16 '12 at 11:12
  • *why isnt that possible*: It is possible. Seems you must be doing something wrong. But you have not showed us *how* you are trying to get the class. – Felix Kling May 16 '12 at 11:13

3 Answers3

5
$(document).click(function (e) {
       console.log('class :' + e.target.className);
});
Fabrizio Calderan
  • 120,726
  • 26
  • 164
  • 177
2

You have few options

call:

.attr('class');

call:

target.className

second will return all classes so if it is main content it will give you string with them it is easier to use

.hasClass("class-name")

eg.

target.hasClass(".hidden")

returns true or false if element hass class or not. Most useful and reliable. Just check if it has your class of choice!

Jakub Oboza
  • 5,291
  • 1
  • 19
  • 10
2

If you are using jQuery for the click event why not use it to get the class attribute

 $(document).click(function () {
       console.log('class :' + $(this).attr("class"));
 });
Arthus
  • 116
  • 8