Click to See Complete Forum and Search --> : Passing value from combo box to function


-asx-
08-17-2005, 10:39 PM
Does anybody know how to pass the value of a combo box selection into a variable in a function? Like, if this is your form, how do you put the value into the variable "selection" so that you can manipulate/process it?? I know this is easy but my brain is apparently working in slow motion right now!


<form>
<select onChange="sort()">
<option value="yankees">Show Yankees</option>
<option value="tigers">Show Tigers</option>
</select>
</form>

<script type="text/javascript">
function sort() {
var selection = ???
-some processing-
}
</script>


Any help is greatly appreciated!!

yearbass
08-18-2005, 02:21 AM
Try this:

<form>
<select onChange="sort(this.options[this.selectedIndex].value)">
<option value="yankees">Show Yankees</option>
<option value="tigers">Show Tigers</option>
</select>
</form>

<script type="text/javascript">
function sort(x) {
var selection = x

alert(selection);
}
</script>

regards

gunar
08-18-2005, 06:42 AM
or better try this:
first get the id of the combobox. Suppose its name is cmbSelection.Then you get the value like this:
var myValue = document.getElementById("cmbSelection").value;
i'm not sure if it's getElementById or getElementsById, but this is how it's done

-asx-
08-22-2005, 10:06 PM
Thank you both for those answers! Both good examples to help me figure out javascript!