Forum Moderators: open
document.form[0].myDropDown.value
in my javascript function.
anyone please advise how can I fetch all the individual values.
Thanks,
Zizo.
<html>
<head>
<script type="text/javascript">function aFunction(elm)
{
var value = elm.options[elm.options.selectedIndex].value // tricky
alert(value)
}
</script>
</head>
<body>
<form>
<select onchange="aFunction(this)">
<option value="apple">apple
<option value="banana">banana
<option value="custard">custard
</select>
</form>
</body>
</html>
So for eg in the code below where I allow user for multiple selections. If user selects apple and banana I need to run the loop in aFunction only for apple and banana.
I hope I am clear here.
<html>
<head>
<script type="text/javascript">
function aFunction(elm) {
var value = elm.options[elm.options.selectedIndex].value // tricky
alert(value)
}
</script>
</head>
<body>
<form>
<select onchange="aFunction(this)" multiple="true">
<option value="apple">apple
<option value="banana">banana
<option value="custard">custard
</select>
</form>
</body>
</html>
<html>
<head>
<script type="text/javascript">function aFunction(elm)
{
var dessertSel = elm.form.elements["dessert"]
var selectedDesserts = getSelections(dessertSel)
// test run through of returned array
for(var k=0;k<selectedDesserts.length;k++)
alert(selectedDesserts[k].value)
}// returns references to the
// actual option elements (not values)
function getSelections(select)
{
var options = select.options,
sOptions = [],
opt, k=0;
while(opt = options[k++])
if(opt.selected)
sOptions[sOptions.length] = opt
return sOptions
}</script>
</head>
<body>Can't use "onchange()" on the select element, since it fires on selection.
Using button instead.<br><b>getSelections</b> returns an array of object references to the option elements,
as opposed to an array of values. This is to make the function more flexible (you
might want to do something to those option elements).<br>To make the function return just values, just add <code>.value</code>
to the 2nd-to-last line.<form>
<select name="dessert" multiple="true">
<option value="apple">apple
<option value="banana">banana
<option value="custard">custard
</select><input type="button" value="get selections" onclick="aFunction(this)">
</form>
</body>
</html>