views:

218

answers:

3

Hi,

I am trying to write code to search all children for a div that has a specific class. The DIV does not have an ID. Here is the HTML I will be using.

<div class="outerBUBGDiv">
<div class="innerBUBGDiv">
<div class="bgHeaderH2">Technology Group</div>
<div class="bgBodyDiv">
<div align="center">
<img height="33" border="0" width="180" src="/heading.jpg"/>
  /////other stuff here/////
</div>
</div>
</div>

Any ideas how I can get the text inside the div with the class bgHeaderH2.

Thanks in advance.

Comment added, didn't explain this very well initially)

+2  A: 
$(this).find(".bgHeaderH2").html();

or

$(this).find(".bgHeaderH2").text();
Ryan
Thanks for the update Ryan, this works as does the other suggestion by Jan.
Caroline
+3  A: 

Based on your comment, moddify this:

$( '.bgHeaderH2' ).html (); // will return whatever is inside the DIV

to:

$( '.bgHeaderH2', $( this ) ).html (); // will return whatever is inside the DIV

More about selectors: http://docs.jquery.com/Selectors

Jan Hančič
You should always use a class selector so 'div.bgHeaderH2' or there will be a performance penalty see http://www.componenthouse.com/article-19
Dave Anderson
@Dave: great tip! didn't event think of that as I was writing this :)
Jan Hančič
+2  A: 

I'm not sure if I understand your question properly, but it shouldn't matter if this div is a child of some other div. You can simply get text from all divs with class bgHeaderH2 by using following code:

$(".bgHeaderH2").text();
Ondrej Slinták