/    Sign up×
Community /Pin to ProfileBookmark

How to append new options to form.select

What Javascript code would let me append new options into a select listbox on a form? Say window.opener.document.formname.selectname …

Andy B.

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@russellMar 10.2005 — <i>
</i>document.myForm.mySelect.options[document.myForm.mySelect.options.length]=new Option(strValue);
Copy linkTweet thisAlerts:
@7studMar 10.2005 — var new_option = document.createElement("option"); //creates a new &lt;option&gt;
var text = document.createTextNode("hat"); //creates the displayed text
new_option.appendChild(text); //puts the text between the &lt;option&gt; tags

new_option.setAttribute("value", "hat"); //gives the option a value attribute equal to "hat"

document.getElementById("my_select").appendChild(new_option); //appends the &lt;option&gt; to the end of the list
Copy linkTweet thisAlerts:
@andyb4authorMar 10.2005 — Thanks so much for the quick help ... works like a charm. I really should get my nose out of php occasionally and get proficient in javascript!

Thanks again!

Andy B.
×

Success!

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