Holy Holy - 1 month ago 9
jQuery Question

jQuery using store selector to find child elements

I often use the

this
keyword within a
foreach
loop to select a child element of
this


$('.width', this).val();


But can I do the same with a stored variable? Something like:

var window = $('#window');
$('.width', window).val();


I would like to store
$('#window')
as it's used often in my code. I thought I could use the above method, but it is not working for me.

UPDATE
The element in question that I'm looking for is:

<div id="window">
<input class="width" type="text" />
</div>

Answer

The syntax

$(child, inContext)

is the same as

$(inContext).find(child)

so yes, you can do

var window = $('#window');
window.find('width').val();

And note that width looks for an element like <width value="xxx"></width>, which seems strange