Can javascript do this in jsp
Hello
Supose that in a form I have one select box. Using Javascript, I can know what option is selected. So in the form I have this
<form name="test">
<select name="test1" onChange="alertselected()">
<option>Option 1</option>
<option>Option 2</option>
</select>
</form>
And the JavaScript is:
function alertselected(){
alert(document.test.test1.selectedIndex)
However, if I have a dynamic list of select boxes. How I can know what option of a selectbox is selected. For example, I have:
<form name="test">
<select name="test1" onChange="alertselected()">
<option>Option 1</option>
<option>Option 2</option>
</select>
<select name="test2" onChange="alertselected()">
<option>Option 1</option>
<option>Option 2</option>
</select>
</form>
thank you
shoa
[1028 byte] By [
sonhoanga] at [2007-10-3 5:35:56]

<form name="test">
<select name="test1" onChange="alertselected('test1')">
<option value"Option 1">Option 1</option>
<option value"Option 2">Option 2</option>
</select>
<select name="test2" onChange="alertselected('test2')">
<option value"Option 3">Option 3</option>
<option value"Option 4">Option 4</option>
</select>
</form>
<script language="javascript">
function alertselected(param)
{
var ind = eval("document.forms[0]."+param+".selectedIndex");
alert(ind);
}
</script>
Cheers
-Rohit
Thanks for your help
It can be seen that, in my example, I have two selectbox so it is easy to have alertselected('test1')" and alertselected('test2"). However, if I have a dynamic list of selectboxes (that means I donot know the size), how I can assign variable for the function alertselected
Thanks
> Thanks for your help
>
> It can be seen that, in my example, I have two
> selectbox so it is easy to have
> alertselected('test1')" and alertselected('test2").
> However, if I have a dynamic list of selectboxes
> (that means I donot know the size), how I can assign
> variable for the function alertselected
>
> Thanks
I wish that you could visualize that you could dynamically generate this select box name and well as parameter to be passed in the javascript.
You can build all the select options in a for loop and assign the name to select box based on each count of the loop.
<select name="test<%=i%>" onChange=alertselected('test<%=i%>')">
-Rohit