Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

onchange - the dropdown selected using jquery

i have a dropdown in the header and whatever the value is selected in the header should reflect the same in the detail dropdown, how should i do that? any leads?

$("#myddl").change(function()
{ 
     //update all the dropdown list...  
});

http://jsfiddle.net/abuhamzah/4QvfP/

  Header:
    <br />
    <select id="myddl" name="myddl">
        <option value="1">One</option>
        <option value="2">Twooo</option>
        <option value="3">Three</option>
    </select>
    <p>Detail</p>
        <br />
    <select id="Select1" name="myddl">
        <option value="1">One</option>
        <option value="2">Twooo</option>
        <option value="3">Three</option>
    </select>
    <br />
    <select id="Select2" name="myddl">
        <option value="1">One</option>
        <option value="2">Twooo</option>
        <option value="3">Three</option>
    </select>
    <br />
    <select id="Select3" name="myddl">
        <option value="1">One</option>
        <option value="2">Twooo</option>
        <option value="3">Three</option>
    </select>
    <br />
    <select id="Select4" name="myddl">
        <option value="1">One</option>
        <option value="2">Twooo</option>
        <option value="3">Three</option>
    </select>
    <br />
    <select id="Select5" name="myddl">
        <option value="1">One</option>
        <option value="2">Twooo</option>
        <option value="3">Three</option>
    </select>
like image 506
Nick Kahn Avatar asked May 08 '12 02:05

Nick Kahn


2 Answers

Something along these lines will work. It wraps your "Detail" selects in a container to make selection somewhat simpler.

http://jsfiddle.net/qMXSR/2/

$("#myddl").change(function()
{ 
     $('.detail').find('select').val($(this).val());
});​
like image 135
Nathan Taylor Avatar answered Oct 13 '22 05:10

Nathan Taylor


Use $.on to bind to the "change" event. From here we target all select elements whose id begins with Select - which includes all of those below. Then we set their value(s) to that of the current one.

​$("#myddl").on("change", function(o){
   $("select[id^=Select]").val(o.target.value);
});​​​​​​​​​​
like image 43
Sampson Avatar answered Oct 13 '22 04:10

Sampson