user3214269 user3214269 - 6 months ago 9
Javascript Question

How to make the select box value back to the default value

Here I want to make the select box value back to the default value.

$(".reportingto").change(function() {
var reportingtovalue = $(this).val();
var reportingtoid=$(this).attr('id');
$('select[id^="reportingto"]').each(function() {
if(reportingtoid!==$(this).attr('id')&& reportingtovalue===$(this).val()){
alert("Duplicate found");
$(reportingtoid).val( $(reportingtoid).prop('defaultSelected') );
}

});

Answer

If the default value is the first/top one, this solution will work for you.

// Replace this
$(reportingtoid).val( $(reportingtoid).prop('defaultSelected') );

// With this
$('#'+reportingtoid).prop('selectedIndex',0);
Comments