Reputation: 3506
Given a dropdown with id of ddlSomething
inside of an aspx form, clicking a reset button (<input type='reset'...
) resets all dropdowns to index of 0. How do I revert the dropdown to the selected index it had immediately after the postback before the user started monkeying around with the form assuming that I reset the form by using javascript to reset all values to empty and all drop down indexes to 0?
Please note that I am NOT asking how to reset the form. I know how to do this.
I am NOT asking how to use jquery or javsacript to set the selected index to 0. I know how to do this.
I am looking to reset the dropdown to the selected index it had immediately after a postback and after using jquery to reset all values to default (nothing in textboxes and selected index of 0 for dropdowns).
Example code for kicks and giggles:
...
<select id='ddlSomething'>
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="mercedes">Mercedes</option>
<option value="audi">Audi</option>
</select>
<input type="button" value="ClearValues" onclick="ClearForm();" />
<input type="reset" value="Reset Values" id="resetAll" class="ResetButton" />
...
...
function ClearForm() {
//handle text
jQuery("input[type='text'],input[type='number'],input[type='email'],input[type='tel']").each(function () {
jQuery(this).val("");
});
jQuery("option:selected").each(function () {
jQuery(this).removeAttr('selected');
});
jQuery("select option:first-child").each(function () {
jQuery(this).attr("selected", "selected");
});
}
Upvotes: 1
Views: 879
Reputation: 3379
I would create a hidden field within the page and set it's value to the selected index of the drop down during the postback. Then in the JS code, you could refer to it, read it's value and set the index on the drop down. Maybe not the cleanest solution, but it should do the job.
Upvotes: 1