Reputation: 58322
I am writing a small script to test if a form has been altered before it has been submitted. So normal inputs (text, textarea, etc) I can use:
if(element.defaultValue != element.value) {
altered[element.name] = element.value;
element.value = element.defaultValue;
}
This works fine. But it appears the select inputs don't have one to check against. Although in Firebug it does appear in the DOM listing, but in black (instead of green) which I believe means that it's added by the browser (correct me if I'm wrong).
If I log the element.defaultValue for a select element it returns undefined
.
So my question, does select have a defaultValue attribute? Or some alternative that I can leverage?
Upvotes: 2
Views: 1396
Reputation: 11
The default option has the attribute defaultSelected set to true.
You can use the following to get the default selection in JQuery:
$('#ddlReceivingStore').children('option[defaultSelected="true"]')
Upvotes: 1
Reputation: 54854
If you are not setting a default selected option on the <select>
element (or if you are always selecting the first option as the default), you can try:
if(element.selectedIndex && element.selectedIndex != 0) {
//this handles <select> inputs
altered[element.name] = element.options[element.selectedIndex].value;
element.selectedIndex = 0;
}
else if(element.defaultValue != element.value) {
//this handles any other kind of input (except not really for checkbox, radio, etc.)
altered[element.name] = element.value;
element.value = element.defaultValue;
}
Upvotes: 1