/    Sign up×
Community /Pin to ProfileBookmark

modifying options in a select box

I am wondering if it is possible to modify the number of options in a select box using JS. This is what I have:

[code]html: <select id=”WEAPONSA” onchange=calc()>
<option value=”none”>no SA
<option value=”none”>no SA
<option value=”none”>no SA
<option value=”none”>no SA
<option value=”none”>no SA
</select>

javascript:
var weaponsa=document.forms[‘statcalculator’].WEAPONSA;
weaponsa.options[1] = new Option(WEAPON[weapon][3],WEAPON[weapon][3],false,weaponsa.options[1].selected);
weaponsa.options[2] = new Option(WEAPON[weapon][5],WEAPON[weapon][5],false,weaponsa.options[2].selected);
weaponsa.options[3] = new Option(WEAPON[weapon][7],WEAPON[weapon][7],false,weaponsa.options[3].selected);
weaponsa.options[4] = new Option(WEAPON[weapon][9],WEAPON[weapon][9],false,weaponsa.options[4].selected);[/code]

Now, what I’m trying to do is make the select box have differing numbers of options. Most of the time weaponsa.options[4] actually has no options at all in my form except in special cases. So, most of the time weaponsa.options[4] ends up as a blank space, which is unseemly. Is there a way to remove weaponsa.options[4] when there is no value for the selection, so that nothing displays and it cannot be selected?

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@scragarNov 15.2006 — you could just test the last box to see if it's empty and if so delete it by going:
weaponsa.options.length--;
×

Success!

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