/    Sign up×
Community /Pin to ProfileBookmark

Is it possible to do an onclick event with option elements inside a select tag? If so how?

I have tried the following html, but when I click the option, nothing happens:

[code=php]<select size=”3″>

<option onclick=”alert(‘hi!’);”>option 1</option>
<option onclick=”alert(‘hi!’);”>option 2</option>

</select>[/code]

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@bulgarian388Mar 10.2007 — Try an onchage event on the select tag and it should work. Your not technically clicking on the option. You are clicking on the select so the onclick events you have will never work.
Copy linkTweet thisAlerts:
@Mr_JMar 10.2007 — You can go like this

<select size="3" onclick="alert(this[selectedIndex].text)">

<option>option 1</option>

<option>option 2</option>

<option>option 3</option>

</select>

But onchange is usually the way to go
Copy linkTweet thisAlerts:
@bear-98765authorMar 10.2007 — It worked! Thanks.
×

Success!

Help @bear-98765 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.19,
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,
)...