DenaliHardtail
DenaliHardtail

Reputation: 28336

How do I get the class of a parent in jQuery?

How do I get the name of the class in the parent ? I have "test stuff" and therefore can get it's parent. Once I have the parent. how do I get the class property?

<div class="ABC">test stuff</div> 

$(this).parent() - gets the DIV with class ABC

Upvotes: 1

Views: 111

Answers (2)

Michael Low
Michael Low

Reputation: 24506

$(this).parent().attr('class') is a way to do it. If the element could potentially have more than one class, you might find the .hasClass() method better.

Upvotes: 1

mattsven
mattsven

Reputation: 23303

$(this).parent()[0].className;
$(this).parent().attr("class");

Also check out the other class manipulation functions.

  1. hasClass()
  2. removeClass()
  3. addClass()

Upvotes: 0

Related Questions