0

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'?

calebo
  • 3,312
  • 10
  • 44
  • 66
  • 1
    please do some *research* before asking. Start here: http://docs.jquery.com/Tutorials:Getting_Started_with_jQuery – ahren Dec 17 '12 at 01:16

2 Answers2

3

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

var b = a.find('.child');
elclanrs
  • 92,861
  • 21
  • 134
  • 171
Godwin
  • 9,739
  • 6
  • 40
  • 58
2

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');

BBagi
  • 2,035
  • 1
  • 19
  • 23