PDA

View Full Version : <select> Drop Down Menu Syntax


peterinwa
03-26-2003, 05:45 AM
In creating a drop down menu I presently use this code:

<option selected value=" + Value + ">" + Text;

Value and Text were taken from the last option the user selected and stored in a cookie and so when the user returns the last option they selected has remained selected. But when the user opens up the drop down menu, they're at the top even if the item was half way down.

Can I instead do this by specifying the position of the item in the menu (array)?

What would be the syntax?

And when the user opens the drop down menu, will it be positioned there? Or will it be positioned at the top of the list again even if the item's half way down?

Thanks, Peter

glenngv
03-26-2003, 07:44 AM
is this what you wanted?


function setSelect(){
var objSel = document.yourFormName.yourSelectName;

//get Value from cookie here

for (i=0;i<objSel.options.length;i++){
if (Value==objSel.options[ i ].value) {
objSel.selectedIndex=i;
break;
}
}
}


you can save only the value of the option in the cookie
call that on onload of the window.

peterinwa
03-26-2003, 04:17 PM
Thanks Glenn. Works great... just what I wanted. But I have a couple questions.

First, you can read a selected item's value and text. Isn't there a way to straight-forward read it's position in the list?

My book has select.selectedIndex and seems to say it does that, but I can't get it to work. I tried:

parent.image.document.f1.list.selectedIndex=43;
alert(parent.image.document.f1.list.select.selectedIndex);

And got the error message from the alert that it wasn't an object.

I guess you can't read the position directly so your routine searches for the matching value to find it. Seems like a long way around, and it would fail if you had two matching values (which my list won't -- or I could use text instead of value I guess).

Thanks again, Peter

cheesebagpipe
03-27-2003, 12:31 AM
Peter...what's this?

alert(parent.image.document.f1.list.select.selectedIndex);

parent = frameset document
image = frame (window) object
document = Document object
f1 = Form object
list = Select object
selectedIndex = Select.selectedIndex property (integer)

Sometimes objects are referenced in a generic sense: Form.onsubmit, e.g., or HTMLElement.onmouseover. The capitalization is a giveaway that the word is a placeholder for an actual object reference, usually involving a name/id (or a method like getElementById).

http://www.devguru.com/Technologies/ecmascript/quickref/select.html