Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Adding an option to a select box 1

Status
Not open for further replies.

wood

MIS
Aug 3, 2000
139
CA
I got my script to remove an option from the select box. Now how do i add an option.
 
I think it is something like:
Code:
var name = "text";
var value = "text";

document.myForm.mySelect.options[document.myForm.mySelect.options.length] = new Option(name,value);
where 'name' is the text that would have normally been placed between the OPTION tags and 'value' is the value that would have normally been set in the OPTION tag's attribute:
Code:
<option value=&quot;text&quot;>
Petey
 
<SCRIPT LANGUAGE=&quot;JavaScript&quot;>
<!--
// You create a new OPTION with
var opt = new Option('text', 'value');

// and insert it with
var sel = document.formName.selectName;
sel.options[sel.options.length] = opt;

// at the end of the SELECT list.
// To insert into the middle move other options up e.g.
function insertOptionAt (select, option, index) {
for (var i = select.options.length; i > index; i--)
select.options
Code:
[i]
= select.options
Code:
[i - 1]
;
select.options
Code:
[index]
= option;
}

// example call
insertOptionAt (document.formName.selectName, new Option('text', 'value'), 3);

// You delete an OPTION simply be setting
var sel = document.formName.selectName;
sel.options
Code:
[optionIndex]
= null;

//-->
</SCRIPT>

- tleish
 
can someone explain this to me, i've looked at the posts on this and still cannot figure this out! i have the forms done like this just to figure this out...can someone explain to me the <javascript>? thanks

<SELECT name=&quot;Year&quot; id=&quot;Year&quot; size=&quot;1&quot; onchange=&quot;selMonth()&quot;>
<OPTION value=&quot;1&quot;>1975</OPTION>
<OPTION value=&quot;1&quot;>1976</OPTION>
<OPTION value=&quot;1&quot;>1977</OPTION>
</SELECT>
<SELECT name=&quot;Month&quot; id=&quot;Month&quot; size=&quot;1&quot; onchange=&quot;selDay()&quot;></SELECT>
<SELECT name=&quot;Day&quot; id=&quot;Day&quot; size=&quot;1&quot;></SELECT>
<SCRIPT LANGUAGE=&quot;JavaScript&quot;>
</SCRIPT>
 
You are missing a couple of auxillary scripst with this but here goes:

<SELECT name=&quot;Year&quot; id=&quot;Year&quot; size=&quot;1&quot; onchange=&quot;selMonth()&quot;>
selMonth() is probably a script to generate a list of month's.
<OPTION value=&quot;1&quot;>1975</OPTION>
<OPTION value=&quot;1&quot;>1976</OPTION>
<OPTION value=&quot;1&quot;>1977</OPTION>
</SELECT>
<SELECT name=&quot;Month&quot; id=&quot;Month&quot; size=&quot;1&quot; onchange=&quot;selDay()&quot;></SELECT>
selDay() is a script that populates the drop down with the proper days in the month. 28 if Feb 31 in march etc.[/red]
<SELECT name=&quot;Day&quot; id=&quot;Day&quot; size=&quot;1&quot;></SELECT>
<SCRIPT LANGUAGE=&quot;JavaScript&quot;>
</SCRIPT>

Since this is javascript, the changes are made without refreshing the screen and returning to the server.[/red] The only dumb questions are the ones that are never asked
 
alright, i understand that, but what goes inside the javascripts that exec the month and day stuff...?i'm new to js, so that's why i'm slow, i'm pretty fluent in perl, but like that matters in js =P..can you explain to me how i would go about making the js for the selDay() and month()?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top