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

Drop Down Selected from If Statement?

Status
Not open for further replies.

EndEffect

Programmer
Jul 11, 2005
2
US
I am attempting to automatically select an option from a javascript generated dropdown box upon returning to the page from error. I am passing javascript an ASP sessioned variable to perform the check.

When I check the one value against the other, I get a true, and my code should execute properly, but for some reason, enclosing the "selected" statement inside the "if" causes the statement to break.

Code follows:

Code:
for ( x = 0 ; x < ProdArrayCnt + 1 ; x++ ) {
     myEle = document.createElement("option");
     myEle.value = JSArrayProd[x][1];
     myEle.text = JSArrayProd[x][2];
     testvalue1 = JSArrayProd[x][1];
     testvalue2 = '<%= Session("prodbox") %>';
     if (testvalue1 == testvalue2){
          myEle.selected = true;
     }
     myEle.style.background = '#bfd8df';
     masterform.prodbox.add(myEle);
}
Can anyone tell me why the myEle.selected craps out? Any help would be greatly appreciated!

EndEffect
 
try this:

Code:
var selectThisOne;
for ( x = 0 ; x < ProdArrayCnt + 1 ; x++ ) {
     myEle = document.createElement("option");
     myEle.value = JSArrayProd[x][1];
     myEle.text = JSArrayProd[x][2];
     testvalue1 = JSArrayProd[x][1];
     testvalue2 = '<%= Session("prodbox") %>';
     if (testvalue1 == testvalue2){
          selectThisOne = x;
     }
     myEle.style.background = '#bfd8df';
     masterform.prodbox.add(myEle);
}
masterform.prodbox.selectedIndex = selectThisOne;

*cLFlaVA
----------------------------
[tt]your mom goes to college[/tt]
[URL unfurl="true"]http://www.coryarthus.com/[/url]
[banghead]
 
AHA!!!!

Muchas Gracias!!!!!

Worked like a charm!

Thank you so much!
 
I tested this:
-----------------------------------
<html>
<head>
<script>
var sessionVar = 'value2';// your session variable value
var JSArrayProd = new Array();

JSArrayProd[0] = new Array();
JSArrayProd[0] = ['value1','text1'];
JSArrayProd[1] = new Array();
JSArrayProd[1] = ['value2','text2'];
JSArrayProd[2] = new Array();
JSArrayProd[2] = ['value3','text3'];
JSArrayProd[3] = new Array();
JSArrayProd[3] = ['value4','text4'];

function doSelect(){
for ( x = 0 ; x < JSArrayProd.length ; x++ ) {
myEle = document.createElement("option");
myEle.value = JSArrayProd[x][0];
myEle.text = JSArrayProd[x][1];
testvalue1 = JSArrayProd[x][0];
testvalue2 = sessionVar;
if (testvalue1 == testvalue2){
myEle.selected = true;
}
myEle.style.background = '#bfd8df';
masterform.prodbox.add(myEle);
}
}
</script>
</head>
<body onLoad="doSelect()">
<form name="masterform">
<select name="prodbox">
</select>
</form>
</body>
</html>
-----------------------------------
Works fine in IE but not in FireFox or Netscape. Those don't like the 'masterform.prodbox.add(myEle);' line. But it's basically what you have so yours should work in IE.

- shu
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top