/    Sign up×
Community /Pin to ProfileBookmark

Writing Options with JS

I have several html selects that need to be populated with options containing numeric values. I have written the js function but I don’t know how to call them between the <select> and </select> tags.

Any help?

to post a comment
JavaScript

7 Comments(s)

Copy linkTweet thisAlerts:
@ExuroJun 09.2004 — To add a new option use something along the lines of the following:

[size=2]
&lt;html&gt;
&lt;head&gt;
&lt;script type="text/javascript"&gt;
&lt;!--
// Note that the 'optionIndex' and 'selected' parameters are optional
function addOption(selectId, optionText, optionValue, optionIndex, selected) {
var sel = document.getElementById(selectId);
selected = selected || false;
optionIndex = optionIndex || sel.options.length;
[color=red]sel.options[optionIndex] = new Option(optionText,optionValue,selected);[/color]
}
//--&gt;
&lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;form&gt;
&lt;p&gt;&lt;select id="selectId"&gt;&lt;/select&gt;&lt;/p&gt;
&lt;p&gt;Option Text: &lt;input type="text" id="optionText" /&gt;&lt;/p&gt;
&lt;p&gt;&lt;input type="button" value="Add Option" onclick="addOption('selectId',this.form.optionText.value,'value',null,true);" /&gt;&lt;/p&gt;
&lt;/form&gt;
&lt;/body&gt;
&lt;/html&gt;
[/size]
Copy linkTweet thisAlerts:
@bobbabuoyauthorJun 09.2004 — Here is what I was trying to do:
<i>
</i>&lt;head&gt;

function writeTime(min, max)
{
for(i = min; i &lt;= max; i++)
document.write("&lt;option&gt;"+i+"&lt;/option&gt;");
}
&lt;/head&gt;


<i>
</i>&lt;select name="time_min" id="time_min"&gt;
&lt;!--javascript function call here--&gt;
&lt;/select&gt;


Can you show me how to incorporate this into your suggestion?

Thanks
Copy linkTweet thisAlerts:
@ExuroJun 10.2004 — Can you call the function at the body's onload event rather than from within the select? Also, does it matter which option is selected initially?
Copy linkTweet thisAlerts:
@bobbabuoyauthorJun 10.2004 — It really doesn't matter which option is selected initially-I would prefer the first one which will happen by default if none is specified, right?

The problem with calling it in the onload event is that I need to call it several times with the max and min variables taking on different values.

Hope this helps you help me...........
Copy linkTweet thisAlerts:
@ExuroJun 10.2004 — [i]Originally posted by bobbabuoy [/i]

[B]The problem with calling it in the onload event is that I need to call it several times with the max and min variables taking on different values.[/B][/QUOTE]

Does that mean that you will be clearing the contents of the <select> element each time you call the function? If so, the script will have to be altered to do that as well...
Copy linkTweet thisAlerts:
@vwphillipsJun 10.2004 — Have you considered re making the selection and innHTML ing it into a div or span

see

http://homepage.ntlworld.com/vwphillips/PreviousEntry/PreviousEntry.htm

for an example
Copy linkTweet thisAlerts:
@bobbabuoyauthorJun 10.2004 — I had not considered that but I will look at it. Thanks!
×

Success!

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