Click to See Complete Forum and Search --> : Need help in accessing Form data


jcoutur
03-13-2003, 11:27 AM
I have a JavaScript function where I would like to reset the number of Selected rows in a Select List. The Select List is inside a table and it is declared as a FORM. I know that the function is called because the first alert is executed, however, the code that accesses the selectedIndex doesn't seem to get executed. I've verified my syntax against examples and I can't figure out why this doesn't work. Hopefully, someone can point out what I'm doing wrong.

The SelectList declaration:
<table>
<tr>
<td>
<FORM name="pnp">
<SELECT MULTIPLE NAME="PGRNAME" size="10" onChange="redirect(this);" >
<%
String val;
if (progname.size() == 0) out.println(blank);
for (Iterator it=progname.iterator(); it.hasNext();) {
val = (String)it.next();
if (prognameSel.get(val) != null) out.println("<OPTION selected>"+val+"</OPTION>");
else out.println("<OPTION>"+val+"</OPTION>");
}
%>
</SELECT></FORM></TD>

..........the rest of the table goes here


The function:
function doReset() {
alert("In doReset"); // this alert shows up
var w = document.pnp.PGRNAME.selectedIndex;
alert("selectedIndex " + w);
var selected_text =document.pnp.PGRNAME.options[w].text;
alert("selectedText " + selected_text);

for (var i=0; i < document.pnp.PGRNAME.length; i++) {
alert( document.pnp.PGRNAME.options[i].text);
}

}


What I really want to do inside this function is to reset all selected rows, ie.
document.pnp.PGRNAME.selectedIndex=-1;

Any help would be greatly appreciated.

gil davis
03-13-2003, 11:54 AM
When you declare a SELECT object as MULTIPLE, selectedIndex is not valid. You have to loop through all of the options and test/clear their SELECTED property.for (var i=0; i<=document.pnp.PGRNAME.options.length; i++)
{document.pnp.PGRNAME.options[i].selected = false;}

jcoutur
03-13-2003, 12:55 PM
Thank you for the reply. I have tried your solution and unfortunately it still doesn't work. I've added an alert inside the for loop and it doesn't appear to get in the loop. I must have something else wrong, although the objects are named as specified so it should work.....

requestcode
03-13-2003, 01:23 PM
Would this work:
<html>
<head>
<title>Drop Down Test</title>
<script language="javascript">
function Doreset(frmobj)
{
frmobj.drop1.selectedIndex=null
}
</script>
</head>
<body bgcolor="lightgreen">
<form name="myform">
<input type="text" name="txt1" size="10"><br>
<select multiple name="drop1" size=4>
<option value="">Select</option>
<option value="1">One</option>
<option value="2">Two</option>
<option value="3">Three</option>
</select>
<input type="button" value="Reset Drop Down" onClick="Doreset(this.form)">
</form>
</body>
</html>

requestcode
03-13-2003, 01:49 PM
This script is probably the better way to go than the last one I posted:
<script language="javascript">
function Doreset(frmobj)
{
len=frmobj.drop1.length
for(i=0;i<len;i++)
{
frmobj.drop1.options[i].selected=false
}
}
</script>