Matt Andrews
Matt Andrews

Reputation: 2878

jQuery: Grab the next <select> with a specific class in a table row

I have this HTML structure (excerpt):

<td>
    <select id="test1" class="pub_chooser">
        <option value="99">All Publications</option>
        <option value="40">Contract</option>
        <option value="21">Student</option>
    </select>
</td>

<td>
    <select id="thing1" class="ev_chooser"></select>
</td>

This is hooked up to an AJAX function to populate the second <select> when an option is clicked.

I'm also allowing users to clone() the table row. I now need to target the second <select> in my AJAX function.

How can I reliably get the next .ev_chooser <select> element when the pub_chooser <select> is clicked?

I've tried this to no avail:

corresponding_select = $('select .ev_chooser').nextAll();

Any tips?

-Matt

Upvotes: 0

Views: 402

Answers (3)

TeKapa
TeKapa

Reputation: 546

corresponding_select = $('pub_chooser').closest('td').siblings().children('.ev_chooser');

Upvotes: 1

jantimon
jantimon

Reputation: 38180

This will give you the last select.

$('select.ev_chooser:last')

Or just search within the cloned element:

var clonedOBJECT = $(whatEverYouClone).clone();
var theSelect= $( 'select.ev_chooser', clonedOBJECT );

Upvotes: 2

reko_t
reko_t

Reputation: 56460

Get rid of the space in the selector:

$('select.ev_chooser')

If you have space between select and the class, it searches all children of select for any element with class "ev_chooser".

Upvotes: 0

Related Questions