Click to See Complete Forum and Search --> : Adding checkbox values to a text field


scouse
06-08-2006, 10:45 AM
Hi

I have a page with checkboxes on for users to select which services they want. When someone checks a checkbox it appends it to a textbox. At the moment I have accomplished this by using the following code:



<script language="JavaScript" type="text/javascript">
<!--
function addser(service)
{
addservice = document.CONTACTFORM.services.value;
addservice = addservice+","+service
document.CONTACTFORM.services.value = addservice
}
//-->
</script>




and then I have a lot of checkboxes with the following on:

<input type='checkbox' value='Asphalt' NAME='SER1' onclick='javascript:addser(this.value);'>
<input type='checkbox' value='Blinds' NAME='SER2' onclick='javascript:addser(this.value);'>
<input type='checkbox' value='Boiler Flues' NAME='SER3' onclick='javascript:addser(this.value);'>

etc

So for example if those three checkboxes were checked the value of the input box "services" would be ",Asphalt,Blinds,Boiler Flues"

However, I need it to be able to remove the relevent value (e.g. Asphalt) if the checkbox is unchecked.

Is this possible?

Cheers

phpnovice
06-08-2006, 11:10 AM
However, I need it to be able to remove the relevent value (e.g. Asphalt) if the checkbox is unchecked.

Is this possible?
Yes, but what you basically end up having to do is to rebuild the entire content of the text box every time. Thus, you can either loop through the form's elements collection to identify those type=="checkbox" elements for inclusion, or you can create a control array of just the check boxes you wish to include in the process and loop through that.

Charles
06-08-2006, 11:11 AM
Yes, but what you basically end up having to do is to rebuild the entire content of the text box every time. Thus, you can either loop through the form's elements collection to identify those type=="checkbox" elements for inclusion, or you can create a control array of just the check boxes you wish to include in the process and loop through that.That's one way to do it, and perhaps the best. Another way would be to use regular expressions to remove the now offending text.