Holy Holy - 11 months ago 45
jQuery Question

jQuery using store selector to find child elements

I often use the

keyword within a
loop to select a child element of

$('.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
as it's used often in my code. I thought I could use the above method, but it is not working for me.

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

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

Answer Source

The syntax

$(child, inContext)

is the same as


so yes, you can do

var window = $('#window');

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