Gaurav
Gaurav

Reputation: 8487

accessing class name by jquery

I have a div with 3 classes like:

   <div class = "a b c"> .. </div>

Now i want to access only class b by .attr(), is it possible?

Upvotes: 0

Views: 76

Answers (2)

ismaels
ismaels

Reputation: 46

You can use something like this:

var classB = $('#element_id').attr('class').split(' ')[1];

Upvotes: 1

Pointy
Pointy

Reputation: 414006

I don't know what you mean by "access". You can check to see if an element has a class:

if ($(elem).hasClass("b")) { ... }

or

if ($(elem).is(".b")) { ... }

You can look for your element by that class with a simple selector:

var with_class_b = $('.b');

It's generally a good idea to not mess with the "className" property (not attribute) yourself if you're using jQuery. You can, of course, but there's really no point.

Upvotes: 5

Related Questions