Click to See Complete Forum and Search --> : get <select> value


goatboy
08-14-2003, 10:46 AM
hi,
I'm trying to get the value of a dropdown box when it changes. the value will then be used as a varible somewhere else, for testing I'm just trying to display the new value in an alert box. I've tried a whole load of ways, here is my latest 'attempt'
function getLocation(loc){
loaction=loc;
alert(location);
}

Location<select name="region"onchange="getRegion(new value? ?)" >
<option>England
<option >Ireland
<option>Scotland
<option>Wales
</select>

obviously this is'nt working, but am I close? any help would be much appreciated.
thanks

blade1
08-14-2003, 11:18 AM
Try this:

<html>
<head>
<script language = "JavaScript">
function getLocation(loc){
alert(loc);
}
</script>
</head>
<body>
Location <select name="region" onchange="getLocation(this.value);" >
<option value = "England">England </option>
<option value = "Ireland">Ireland </option>
<option value = "Scotland">Scotland </option>
<option value = "Wales">Wales </option>
</select>
<body>
</html>

goatboy
08-14-2003, 12:25 PM
cheers Blade, but alas.. no joy :( tried it but it gives an error.
I also noticed that in my first post I'd named the function getLocation but was calling getRegion.. just a typo!

anything else I could try?

blade1
08-14-2003, 12:49 PM
Hmmm...working ok for me in IE 6.0. Can you tell me what browser you're using and what error message you're getting?

goatboy
08-14-2003, 12:55 PM
I'm using IE 5. I just get the error symbol in the status bar which appears when I choose an option from the dropdown.

blade1
08-14-2003, 01:06 PM
<html>
Unfortunately, corporate policies do not allow me to download your browser so that I can have a definite answer for you, but I'm thinking maybe IE 5.0 may be complaining because there isn't a form defined. (just a guess...)

If you click on the yellow hazard sign, it will give you the error that it's finding. If this doesn't work, maybe you can provide that for me?

<head>
<script language = "JavaScript">
function getLocation(loc){
alert(loc);
}
</script>
</head>
<body>
<form name = "form1">
Location <select name="region" onchange="getLocation(document.form1.region.options[document.form1.region.selectedIndex].value);" >
<option value = "England">England </option>
<option value = "Ireland">Ireland </option>
<option value = "Scotland">Scotland </option>
<option value = "Wales">Wales </option>
</select>
</form>
<body>
</html>

goatboy
08-14-2003, 01:18 PM
appreciate this bud.

I tried that code and the the error message I'm getting is 'object expected'. is that of any use?

blade1
08-14-2003, 01:34 PM
Well, if this doesn't work, I'll have to throw in the towel...I ran this in Netscape 4.79 with no problems and that's the crappiest browser ever so I'd be surprised if this doesn't work. Is this the only code that's in your page? Is there anything else that could be blowing up?

<html>
<head>

<SCRIPT language=JavaScript>
function getLocation(loc){
var comboValue
var selIndex = document.frmName.select1.selectedIndex;
comboValue = document.frmName.select1.options[selIndex].value;
alert(comboValue);
}
</SCRIPT>


</head>
<body>
<form name = "frmName">
Location <select name="select1" onchange="getLocation();" >
<option value = "England">England </option>
<option value = "Ireland">Ireland </option>
<option value = "Scotland">Scotland </option>
<option value = "Wales">Wales </option>
</select>
</form>
</body>
</html>

diamonds
08-14-2003, 01:37 PM
Try this:
document.form1.region.selectedindex[document.form1.region.selectedIndex].value

blade1
08-14-2003, 01:45 PM
Oops, regarding my last post, please take the "loc" parameter out of the Javascript function.

goatboy
08-14-2003, 02:33 PM
YES YES YES YES! <round of applause> Blade, thanks m8. although I'm kinda embarassed coz your second suggestion worked perfect , my fault :rolleyes: sorry for the extra work u done. nonetheless its been a great help and I can now get on with this project, so thanks.
diamonds, thanks for the suggestion as well :D

blade1
08-14-2003, 02:40 PM
Good deal! Good luck with the rest of your project. :)