javascript - set selected value for dropdowns -
the below code works 1 time not every time
var selectedval = $('#drpgender_0').find("option:selected").text(); if (selectedval == "male") { $('#drpgender_1').find("option:contains('male')").text('male').attr("selected", false); $('#drpgender_1').find("option:contains('female')").text('female').attr("selected", "selected"); } else if (selectedval == "female") { $('#drpgender_1').find("option:contains('female')").text('female').attr("selected", false); $('#drpgender_1').find("option:contains('male')").text('male').attr("selected", "selected"); } };
thanks in advance
$('#drpgender_0').on('change', function() { var selectedval = $('#drpgender_0').val(); if (selectedval == "male") { $('#drpgender_1').val('female'); } else if (selectedval == "female") { $('#drpgender_1').val('male'); } });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <select id="drpgender_0"> <option value="">select gender</option> <option value="male">male</option> <option value="female">female</option> </select> <select id="drpgender_1"> <option value="">select gender</option> <option value="male">male</option> <option value="female">female</option> </select>
you can use code
$('#drpgender_0').on('change', function() { var selectedval = $('#drpgender_0').val(); if (selectedval == "male") { $('#drpgender_1').find("option:contains('female')").attr("selected", "selected"); } else if (selectedval == "female") { $('#drpgender_1').find("option:contains('male')").attr("selected", "selected"); } });
Comments
Post a Comment