/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] .defaultValue for a dropdown?

I’m relatively new to javascript. I know you can find the default (original) value of a text input field by using this.defaultValue. Is there an equivalent property for a dropdown list (<select><option>), or radio button list or checkbox list? .defaultValue doesn’t seem to work.

I can simply add a hidden field to keep track of the value but I’m not sure if there is a better way to do it.

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@mataichiMay 18.2009 — Radio & Checkbox = defaultChecked

Doesn't look like drop down list has one.

Not sure if this is what you are looking for, because I think this will only tell you if any individual input was checked or not, not for the entire group what was checked. Could be wrong on this though.
Copy linkTweet thisAlerts:
@Y_LessMay 18.2009 — Just save the select tag's value when the page first loads:

var selectDefault = selectTag.value;

Then you can use that later, even if it changes:

if (selectTag.value != selectDefault) // Value has changed
Copy linkTweet thisAlerts:
@VolcomMkyMay 18.2009 — Are you talking about the "selected" option in a droplist?

Like..

[code=php]<select name="dropList">
<option value="DefaultValue" selected>Default Item</option>
<option value="AnotherValue">NOT Default Item</option>
</select>[/code]
Copy linkTweet thisAlerts:
@amichaelauthorMay 18.2009 — Thank you both for your replies. You pointed me in the right direction.

There is a defaultSelected property for <option> elements, similar to defaultChecked for radio buttons and checkboxes. But there doesn't appear to be any property to get the value of the collective group of elements (i.e. selectTag.defaultSelected).

I can either loop through the elements or just store the value somewhere, like a hidden field or a var like Y_Less suggested.
×

Success!

Help @amichael spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.20,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...