/    Sign up×
Community /Pin to ProfileBookmark

How to do onclick in opition Tag

Hi,
i am having a code like this

“`
<!DOCTYPE html>

<html>
<head>
<meta charset=”utf-8″>
<title>Doc</title>
</head>
<body>

<select class=”select-1″ id=”select-1″ techsuggest=”s1″>
<option>Select The Brand Here</option>
<option onclick=”nums()”>10</option>
<option>4</option>
<option>7</option>
<option>1</option>
<option>8</option>
<option>9</option>

</select>
<script src=”script.js”></script>
</body>
</html>
“`

In my Script.js file

“`
function nums(){
document.write(“Numbers”)
}

“`

But it doesn’t work

to post a comment
HTMLJavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@SempervivumOct 23.2021 — `document.write` often is a reason for trouble, particularly when used in a function. Try to do without. Please describe what your intention is, what should happen when an option is clicked?
Copy linkTweet thisAlerts:
@boohooOct 23.2021 — &lt;option&gt; elements don't support click events. Instead, you can handle change event on the &lt;select&gt;:

``html<i>
</i>&lt;select class="select-1" id="select-1" techsuggest="s1" onchange="nums(this)"&gt;
&lt;option&gt;Select The Brand Here&lt;/option&gt;
&lt;option&gt;10&lt;/option&gt;
&lt;option&gt;4&lt;/option&gt;
&lt;option&gt;7&lt;/option&gt;
&lt;option&gt;1&lt;/option&gt;
&lt;option&gt;8&lt;/option&gt;
&lt;option&gt;9&lt;/option&gt;
&lt;/select&gt;<i>
</i>
`</CODE>
And then check if it equals to <C>
"10"</C>:

<CODE lang="javascript">
`javascript<i>
</i>function nums(select) {
if (select.value === '10') {
// do something
}
}<i>
</i>
``
×

Success!

Help @Sarvesh_ 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 4.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: @Yussuf4331,
tipped: article
amount: 1000 SATS,

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

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...