/    Sign up×
Community /Pin to ProfileBookmark

How can you add a new item in a SELECT list?

Hello once again…

How can you add a new element in a select list, when you use javascript??

Does anyone know this, because i can’t seem to find it??

Thanks

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@gil_davisMar 12.2003 — A "select" list is an array of "option" objects, and has a property called "length". You can add a new option by creating one and attaching it to the end of the array.<form>
<select name="s1">
<option>--- Select an option ---
<option>Option 1
<option>Option 2
</select>
<input type="button" value=" Add Option " onclick="this.form.s1.options[this.form.s1.options.length]=new Option('Option ' + this.form.s1.options.length)">
</form>
×

Success!

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