2

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?

2 Answers 2

1

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; } 
Sign up to request clarification or add additional context in comments.

2 Comments

I see the path you're taking here, I was hoping to utilise built in properties so I didn't have to maintain seperate variables/arrays. If I go down a similar track to your solution I might use the jquery meta class
This is the closest to the answer that I have found.
1

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"]') 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.