/    Sign up×
Community /Pin to ProfileBookmark

Clicking on Dropdown Label

I learned recently that clicking on a select element’s label will cause the select element to reset. For example, if I have the following:

<label for “color”>Select a color</label>
<select name=”color” id=”color”>
<option selected>Select one</select>
<option>red</option>
<option>blue</option>
<select>

A user chooses, “red”. If the user then clicks on the “Select a color” text, the dropdown will reset to “Select one”.

So, here is my question. Is there, 1. a way to prevent that behavior or 2. a way to attach a function that will execute any time a user clicks a form label?

Thanks in advance

Edit – One other possibility – Could I write a function that would disable the onclick event in all select element labels?

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@gil_davisFeb 14.2008 — This is a stab in the dark, but:&lt;label for "color" onclick="return false"&gt;
Copy linkTweet thisAlerts:
@betheballauthorFeb 14.2008 — Thanks Gil. That works beautifully. Now the next dilemma. Any way to add that behavior other than manually inserting it into the label for each dropdown? I have about 50 files with each containing 20+ dropdowns. I also have a .js file that all these files use. So, as you can imagine, I would love to be able to add something to this common .js file rather than edit every single dropdown.
Copy linkTweet thisAlerts:
@mrhooFeb 15.2008 — If you don't want the labels' behavior, lose the labels. Put the text in a text node beside the select element.

Why include a label if it is always disabled?
×

Success!

Help @betheball 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.18,
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,
)...