calebo
calebo

Reputation: 3442

how to target a class within a variable using jQuery

Not sure if I phrase the question correctly, but I'm trying to target a class within another class, but I've declared the parent class as a variable.

<div class="parent">
  <p class="child">hello world</p>
</div>

var a = $('.parent');
var b = $('.parent .child');

Is there a better way/convetion of declaring variable 'b'? Or targeting classes within variable 'a'?

Upvotes: 0

Views: 1718

Answers (2)

BBagi
BBagi

Reputation: 2085

Since 'a' is now a jquery object, you can use the 'children' method to find a matching node:

var b = a.children('p.child'); or var b = a.children('p');

Upvotes: 2

Godwin
Godwin

Reputation: 9937

If you want to target elements inside of a you can use:

var b = a.find('.child');

Upvotes: 3

Related Questions