select with related action list

M

Mel

i would like set my <Select> options from a list AFTER the form is
created and a list of urls for those selections from javascript. How
can i do this

My select may have a list like: "Go to One", "Go to Two" etc and my
list is like "http://www.yahoo.com", "http://www.google.come"

I will HAVE to do this after the form is created and NOT at creation
time.

Please help with sample code if possible.

ALSO, i dont know how many options are available AHEAD of time.

Many thanks for lifting a newbeeeeeeeeeeeeeeeeee
 
B

brunascle.maps

i would like set my <Select> options from a list AFTER the form is
created and a list of urls for those selections from javascript. How
can i do this

My select may have a list like: "Go to One", "Go to Two" etc and my
list is like "http://www.yahoo.com", "http://www.google.come"

I will HAVE to do this after the form is created and NOT at creation
time.

Please help with sample code if possible.

ALSO, i dont know how many options are available AHEAD of time.

Many thanks for lifting a newbeeeeeeeeeeeeeeeeee

this is the standards-compliant way to do it. it might not be the
simplest way.

var theSelect = document.getElementById("theIdOfTheSelect");

var option1 = document.createElement("option");
option1.setAttribute("value", "the value of option 1");
option1.appendChild(document.createTextNode("the text to display for
option 1"));
theSelect.appendChild(option1);
 
M

Mel

this is the standards-compliant way to do it. it might not be the
simplest way.

var theSelect = document.getElementById("theIdOfTheSelect");

var option1 = document.createElement("option");
option1.setAttribute("value", "the value of option 1");
option1.appendChild(document.createTextNode("the text to display for
option 1"));
theSelect.appendChild(option1);

thanks for your help.
How can i select say item number 2 from the list ? after setting my
options and how can i ask it to go to my url ?
 
B

brunascle.maps

How can i select say item number 2 from the list

.... use your browser

how can i ask it to go to my url ?

onchange is the event that runs when you change the selected item in
the list. and selectedIndex gives you the index of the item that was
selected. so you could do something like this (assuming you're putting
the URL into the value of the options):

<select ...
onchange="window.location=this.options[this.selectedIndex].value;" ...>

you might even be able to just do:
window.location=this.value
but i'm not positive

one problem i bet you'll run into is how would they select the first
item? the first one will probably be selected by default, so if they
want the first item then the onchange event is never going to run.
 
M

Mel

How can i select say item number 2 from the list

... use your browser

how can i ask it to go to my url ?

onchange is the event that runs when you change the selected item in
the list. and selectedIndex gives you the index of the item that was
selected. so you could do something like this (assuming you're putting
the URL into the value of the options):

<select ...
onchange="window.location=this.options[this.selectedIndex].value;" ...>

you might even be able to just do:
window.location=this.value
but i'm not positive

one problem i bet you'll run into is how would they select the first
item? the first one will probably be selected by default, so if they
want the first item then the onchange event is never going to run.

thanks again;

but are you saying that i can not select a dropdown option from
javascript ?
 
E

Evertjan.

Mel wrote on 06 apr 2007 in comp.lang.javascript:
How can i select say item number 2 from the list ? after setting my
options and how can i ask it to go to my url ?

<select
onchange =
'location.href=this.options[this.selectedIndex].value'>
<option value=''>-- choose --</option>
<option value='http://cnn.com/'>CNN</option>
<option value='http://google.com/'>Google</option>
</select>
 
B

brunascle.maps

thanks again;

but are you saying that i can not select a dropdown option from
javascript ?

yes you can. when you do it that way, i dont think the select's
onchange event will run.

theSelect.selectedIndex = 4;
 
B

brunascle

Try testing, extensively, IE's support for setAttribute and the answer
becomes obvious.

mmmmm, yeah i was wondering that as i typed it. i believe it's mostly
just when you use setAttribute() for "value" though, i've never had
any problem with it otherwise. (funny thing, i actually originally
typed ".value" but then i though "i can either change this
to .setAttribute or i change get rid of the words "standards-
compliant).

Also, try looking into the forms collection instead of
relying on gEBI as a crutch.

no thanks, i try to stick to valid DOM unless there's a significant
reason not to.
 
B

brunascle

Huh? What do you think "valid DOM" is?

i'm talking about core DOM, not HTML DOM. who knows what the W3C is
going to do once XHTML catches on, but i'd bet my bottom dollar that
anything written in valid core DOM will continue to function.

The forms collection was around
long before gEBI was ever dreamed up.

and? i doubt you feel the same way about document.write vs.
document.createElement

Using getElementById is becoming
the new age crutch for people who don't understand the more efficient
collections available.

sorry. the times, they are a changin.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top