Forum Moderators: open

Message Too Old, No Replies

How do I reset multi-select boxes?

reset multi-select boxes in a form with javascript

         

majjk

3:19 pm on Feb 29, 2004 (gmt 0)

10+ Year Member



A simplified version of my form looks like this:

<form name="my_form" action="<?php print "$PHP_SELF";?>" method="POST">

<select name="category12[]" multiple="yes">
<option value="1">value 1</option>
<option value="2">value 2</option>
</select>

<INPUT type="button" value="Clear Fields" name="clear12" onClick=clearFields(12)>

<select name="category34[]" multiple="yes">
<option value="3">value 3</option>
<option value="4">value 4</option>
</select>

<INPUT type="button" value="Clear Fields" name="clear34" onClick=clearFields(34)>

<input type="submit" name="my_submit" value="<?php print "$button_send";?>">
</form>

What I would like to do is to reset one of my select boxes with a click on a button calling a javascript function (clearFields) without touching the values of the other select box. I've seen examples on how to reset text boxes, radio buttons etc, but nothing on multi select. Could maybe someone write me 2-3 lines of javascript showing me what my function should look like?

Cheers

Majjk

ajkimoto

5:00 pm on Mar 2, 2004 (gmt 0)

10+ Year Member



Majjk,

How about this:

<head>
<script type="text/javascript">
<!--
function resetSelect(objID){
selObject=document.getElementById(objID)
//cycle through the options collection, setting the selected attribute of each to false
for (i=0;i<selObject.options.length;i++){
selObject.options[i].selected=false;
}

}
//-->
</script>

</head>

<body>
<form method="post" action="">
<select name="mySelect" id="mySelect" multiple="yes">
<option value="1">value 1</option>
<option value="2">value 2</option>
<option value="3">value 3</option>
<option value="4">value 4</option>
</select>
<input type="button" onclick="resetSelect('mySelect')" value="reset" />

</form>
</body>

Hope this helps,

ajkimoto