/    Sign up×
Community /Pin to ProfileBookmark

Adding checkbox values to a text field

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:

[code]

<script language=”JavaScript” type=”text/javascript”>
<!–
function addser(service)
{
addservice = document.CONTACTFORM.services.value;
addservice = addservice+”,”+service
document.CONTACTFORM.services.value = addservice
}
//–>
</script>
[/code]

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

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@phpnoviceJun 08.2006 — However, I need it to be able to remove the relevent value (e.g. Asphalt) if the checkbox is unchecked.

Is this possible?[/QUOTE]

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 [b]elements[/b] collection to identify those [b]type=="checkbox"[/b] 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.
Copy linkTweet thisAlerts:
@CharlesJun 08.2006 — 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 [b]elements[/b] collection to identify those [b]type=="checkbox"[/b] 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.[/QUOTE]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.
×

Success!

Help @scouse spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.24,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...