jquery - how is multiple selection working in this example?

Posted by hatorade on Stack Overflow See other posts from Stack Overflow or by hatorade
Published on 2010-04-22T08:28:30Z Indexed on 2010/04/22 8:33 UTC
Read the original article Hit count: 164

Filed under:
|

The relevant snippet of HTML:

<span class="a">
<div class="fieldname">Question 1</div>
<input type="text" value="" name="q1" />
</span>

The relevant jQuery:

$.each($('.a'), function(){
$thisField = $('.fieldname', $(this));
});

What exactly is being set to $thisField? If my understanding of multiple selectors in jQuery is correct, it should be grabbing the outer <span> element AND the inner <div> element. But for some reason, if I use $thisField.prepend("hi"); it ends up putting hi right before the text Question 1, but not before <div>. I thought multiple selectors would grab both elements, and that prepend() adds hi to the beginning of BOTH elements, not just the <div>

© Stack Overflow or respective owner

Related posts about jQuery

Related posts about multiple-selectors