PDA

View Full Version : testing for a value



mtm
Nov 17th, 2003, 08:01 PM
is this testing for a value in a form field? the field is a dropdown box.

alert(lanForm.p_loc.value)

I'm thinking something selectedIndex here...but don't know what.

TIA

kansel
Nov 17th, 2003, 08:23 PM
If it is a <select> element, you'll need a reference to which option is selected (selectedIndex as you said)

var e = lanForm.p_loc; // set a variable to refer to the form element
// this makes typing the next command quite a lot easier
alert(e.options[e.selectedIndex].value);

And the alert isn't acutally testing for anything, it will just pop up an alert box displaying the value of the selected option. Testing would look more like this:

if(e.options[e.selectedIndex].value == "option1");
// do something when option1 is selected //

Or even better, using a switch()

switch(e.options[e.selectedIndex].value){
case "option1": // do option 1 here //
break;
case "option2": // do option 2 here //
break;
default: // default action //
break;
}

mtm
Nov 17th, 2003, 08:33 PM
thanks kansel.
that was indeed helpful.
tried the first method - at first the alert didn't show.

am working inside a package in pl/sql and have to compile, drop the package and pick it up again - painful.

anyway - the alert is showing now, but there's some other prob. as it doesn't read the value.
not sure if it's naming or just syntax.

am still trying. thanks so much for the helpful and speedy reply!

Willy Duitt
Nov 17th, 2003, 10:08 PM
Try:

var e = document.lanForm.p_loc; // set a variable to refer to the form element
// this makes typing the next command quite a lot easier
alert(e.options[e.selectedIndex].value);