Search code examples
jqueryselectjquery-selectorscoldfusion-8

jquery find value of selected item in drop down with a twist


I have a drop down list displaying values in a select tag that I would normally just use $('#ID').val() to obtain the value but the ID is either unknown or a dynamically added row.

Following the closing select tag, I have an jquery ui icon I'm using that when clicked will capture the value of drop down option currently selected (this value will be replicated to the other select lists). This bit of code loops so I know I'll need to use some sort of .find() or .parent() or something along those lines which is still not sunk in with me yet.

Here is the source code:

<tr>
<th class="form"><label>SI Contact</label></th>
<td id="name_29805"><div style="float: left;">
        <select name="contactsbcuid" id="contactsbcuid_29805" sid="29805" ordr="1">
            <option value="userID1234">Doe, John</option>
            <option value="userID1235">Doe, Jane</option>
            ...
            <option value="userID1236">Smith, David</option>
        </select>
        <input name="orig_contactsbcuid" id="orig_contactsbcuid_29805" value="userID1235" sid="29805" ordr="1" type="hidden">
    </div>
    <div style="float: right;"><span class="ui-icon ui-icon-copy vtip" onClick="CopyDown('contactsbcuid',1)" id="select_contactsbcuid_29805" title="Copy the selected value down the list." style="float: right;"></span></div></td>


Solution

  • It looks like there is only a single select in the tablecell. So it should be sufficient to find the 'td' and when you've found it search for the select.

    var value = $(this).parents('td').find('select').val();