I need to be able to populate a dropdown list with values that are selected (checked) in checkboxes. Here's what I have so far:
It works in the most basic of ways, but I need to be able to prevent duplicates and/or remove values from the list if a box is unchecked. Any help would be appreciated.
Code:
<script type="text/javascript">
<!--
function moveIt(numboxes){
var boxes = new Array(numboxes)
for (var j = 0; j < numboxes; ++j) {
boxes[j] = document.forms['updateuser'].usergroup[j].value;
}
for (var i = 0; i < numboxes; ++i) {
if (document.forms['updateuser'].elements[i].checked){
addOption(document.forms['updateuser'].primarygroup,boxes[i],boxes[i]);
}
}
}
function addOption(selectbox,text,value ){
var optn = document.createElement("OPTION");
optn.text = text;
optn.value = value;
selectbox.options.add(optn);
}
//-->
</script>
<form name="updateuser">
<table border="0" cellpadding="0" cellspacing="2">
<tr>
<th>User Groups</th>
</tr>
<tr>
<td>
<input type="checkbox" name="usergroup" value="first" onclick="if(this.checked){ moveIt(2);}"> first<br />
<input type="checkbox" name="usergroup" value="second" onclick="if(this.checked){ moveIt(2);}"> second<br />
<br /><br /><h4>Primary Group</h4><br />
<select name="primarygroup">
<option value="">none</option>
</select>
</td>
</tr>
</table>
</form>
It works in the most basic of ways, but I need to be able to prevent duplicates and/or remove values from the list if a box is unchecked. Any help would be appreciated.