WIRN
WIRN

Reputation: 947

Get class with jQuery

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?

Upvotes: 1

Views: 220

Answers (3)

Arthus
Arthus

Reputation: 116

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"));
 });

Upvotes: 2

Jakub Oboza
Jakub Oboza

Reputation: 5421

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!

Upvotes: 2

Fabrizio Calderan
Fabrizio Calderan

Reputation: 123387

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

Upvotes: 5

Related Questions