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 wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to use names instead of the array number? 1

Status
Not open for further replies.

bct10

Technical User
May 3, 2004
37
US
hi all
I have a javascript form wir a drom down box
were I went to selected the default value by giving my <option>s different names and use them instead of the array number, here is what I tried an it doex not work
thake for an tips on what I did wrong
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">

<html>
<head>
<title>myweather form</title>

<script language="JavaScript">
function Warnings(){

var url1="var url2="/allwarnings.html"

var box = document.weatherform.stateselter;
var where_to = box.options[box.options.selectedIndex].value

if (where_to == "")
{
alert("select a state")
return
}

else {window.location = url1+where_to+url2;}
}

function Watches(){
var url1="var url2="/watches.html"

var box = document.weatherform.stateselter;
var where_to = box.options[box.options.selectedIndex].value;

if (where_to == "")
{
alert("select a state")
return
}

else {window.location = url1+where_to+url2;}
}



function statecheck(){

document.weatherform.stateselter.name.["George"].selected = true;



}


</script>
</head>

<body onLoad="statecheck()">
<div align="center">
<br><br>
<form name="weatherform">
<select name="stateselter">
<OPTION VALUE="al">AL</OPTION>
<OPTION VALUE="ak">AK</OPTION>
<OPTION VALUE="az">AZ</OPTION>
<OPTION VALUE="ar">AR</OPTION>
<OPTION VALUE="ca">CA</OPTION>
<OPTION VALUE="co">CO</OPTION>
<OPTION VALUE="ct">CT</OPTION>
<OPTION VALUE="de">DE</OPTION>
<OPTION VALUE="fl">FL</OPTION>
<OPTION VALUE="ga">GA</OPTION>
<OPTION VALUE="hi">HI</OPTION>
<OPTION VALUE="id">ID</OPTION>
<option name="George" value="il">IL
<OPTION VALUE="in">IN</OPTION>
<OPTION VALUE="ia">IA</OPTION>
<OPTION VALUE="ks">KS</OPTION>
<OPTION VALUE="ky">KY</OPTION>
<OPTION VALUE="la">LA</OPTION>
<OPTION VALUE="me">ME</OPTION>
<OPTION VALUE="md">MD</OPTION>
<OPTION VALUE="ma">MA</OPTION>
<OPTION VALUE="mi">MI</OPTION>
<OPTION VALUE="mn">MN</OPTION>
<OPTION VALUE="ms">MS</OPTION>
<OPTION VALUE="mo">MO</OPTION>
<OPTION VALUE="mt">MT</OPTION>
<OPTION VALUE="ne">NE</OPTION>
<OPTION VALUE="nv">NV</OPTION>
<OPTION VALUE="nh">NH</OPTION>
<OPTION VALUE="nj">NJ</OPTION>
<OPTION VALUE="nm">NM</OPTION>
<OPTION VALUE="ny">NY</OPTION>
<OPTION VALUE="nc">NC</OPTION>
<OPTION VALUE="nd">ND</OPTION>
<OPTION VALUE="oh">OH</OPTION>
<OPTION VALUE="ok">OK</OPTION>
<OPTION VALUE="or">OR</OPTION>
<OPTION VALUE="pa">PA</OPTION>
<OPTION VALUE="pr">PR</OPTION>
<OPTION VALUE="ri">RI</OPTION>
<OPTION VALUE="sc">SC</OPTION>
<OPTION VALUE="sd">SD</OPTION>
<OPTION VALUE="tn">TN</OPTION>
<OPTION VALUE="tx">TX</OPTION>
<OPTION VALUE="ut">UT</OPTION>
<OPTION VALUE="vt">UV</OPTION>
<OPTION VALUE="va">VA</OPTION>
<OPTION VALUE="wa">WA</OPTION>
<OPTION VALUE="wv">WV</OPTION>
<OPTION VALUE="wi">WI</OPTION>
<OPTION VALUE="wy">WY</OPTION>
</select>
&nbsp;&nbsp;
<input type="button" value="Warnings" onClick="Warnings()">
<input type="button" value="Watches" onClick="Watches()">
</form>
</div>



</body>
</html>
 
Use this in your statecheck function:

document.weatherform.stateselter.options["George"].selected = true;

Lee
 
Or:

document.getElementsByName("George").selected = true;

The previous doesn't select Illinois in IE, but does in Firefox.

Lee
 
Hi lee
I am using IE
I get an error whith this line
document.weatherform.stateselter.options["George"].selected = true;

but no error with

document.getElementsByName("George").selected = true;
but it does not select il what am i missing thanks beau
 
Haven't tried it myself, but maybe:

document.forms['weatherform'].elements['stateselter'].options["George"].selected = true;

will work.

If all else fails, just set the option for IL as selected in the HTML.

Lee
 
Hi lee the prblem i have is i need it to change dinamicily thanks for an ideas
 
How about something like this?

var opt = document.forms['weatherform'].elements['stateselter'].options;

var target = 'il';
for (var oi=0; oi < opt.length;oi++)
{
if (opt[oi].value.tolowercase() == target)
{
opt[oi].selected = true;
break;
}
}

Lee
 
Maybe this assumes less, cross-browser and stands more readily to generalize.
[tt]
function statecheck(){
with (document.weatherform.stateselter) {
for (var i=0;i<options.length;i++) {
if (options.getAttribute("name")=="George") {
selectedIndex=i;
break;
}
}
}
}
[/tt]
If "George" is dynamic, you can pass to the function a string s like this.
[tt] function statecheck(s){
[/tt]
Then locate it like this.
[tt] if (options.getAttribute("name")==s) {
[/tt]
- tsuji
 
Hi tsuji

I should have benn clear
the state is gong to chage every time it loads
could you show me how to do that thinks
 
bct10,

I don't know what state is. Maybe this for some server-side variable state?
[tt]
<body onLoad="statecheck(<%=state%>)">
[/tt]
Then as said above.
[tt]
function statecheck(s){
with (document.weatherform.stateselter) {
for (var i=0;i<options.length;i++) {
if (options.getAttribute("name")==s) {
selectedIndex=i;
break;
}
}
}
}
[/tt]
regards - tsuji
 
hi tsuji in tjis script you see state
how wood i change this line so it would read state if (options.getAttribute("name")=="George") {

thanks
function statecheck(){
var state ="George"
with (document.weatherform.stateselter) {
for (var i=0;i<options.length;i++) {
if (options.getAttribute("name")=="George") {
selectedIndex=i;
break;
}
}
}
}

 
>if (options.getAttribute("name")=="George") {
[tt]if (options.getAttribute("name")==[blue]state[/blue]) {[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top