Reset Select Dropdown Order By Value in jQuery
In this tutorial we are going to learn Reset select dropdown order by value in jquery by using div ID
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
function resetdropdown(divId){ var $dd = $(divId); if ($dd.length > 0) { var selectedVal = $dd.val(); var $options = $('option', $dd); var arrVals = []; $options.each(function(){ arrVals.push({ val: $(this).val(), text: $(this).text() }); }); arrVals.sort(function(a, b){ return a.val - b.val; }); for (var i = 0, l = arrVals.length; i < l; i++) { $($options[i]).val(arrVals[i].val).text(arrVals[i].text); } $dd.val(selectedVal); } } |
Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
Input: <select class="form-control"> <option value="10">postpositives</option> <option value="2">laraphp</option> <option value="8">healthwealth</option> <option value="4">freshersgo</option> <option value="">--Select Type--</option> <option value="9">writemore</option> <option value="3">bugtricks</option> </select> Output: <select class="form-control"> <option value="">--Select Type--</option> <option value="2">laraphp</option> <option value="3">bugtricks</option> <option value="4">freshersgo</option> <option value="8">healthwealth</option> <option value="9">writemore</option> <option value="10">postpositives</option> </select> |
Recent Comments