Search code examples
jquerychildren

jQuery - Cycling through child elements


I have a series of select boxes within a table like so:

<tr>
<td><select></select></td>
<td><select></select></td>
<td><select></select></td>
<td><select></select></td>
</tr>

With about 10 rows.

I'm trying to reset all of the select boxes in the row to a default value, but having trouble with the syntax, can anyone help? This is what I have at the moment, but it dosen't seem to be working:

$(row).children('td > select').each().val('0');

Any advice appreciated.

Thanks.


Solution

  • If you are trying to simply select a default value, this should work (not tested):

    $('select', row).each(function() {
        this.selectedIndex = 0; //or whatever the index you want
    });
    

    Using $('select', row) is a bit faster than the selector you have, so is accessing directly the objects property, instead of using the jQuery object.