•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the JavaScript / DHTML / AJAX section within the Web Development category of DaniWeb, a massive community of 455,964 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,607 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our JavaScript / DHTML / AJAX advertiser: Lunarpages Web Hosting
Views: 2524 | Replies: 4
![]() |
•
•
Join Date: Nov 2007
Posts: 4
Reputation:
Rep Power: 0
Solved Threads: 0
Hello,
I am trying to print all the selected checkbox values to a textbox separated by commas (, ). However, the first value in the textbox always starts with a comma (, ) i.e. (, one, two, three) etc. My current script trys to check if the first 2 characters (substring) are ", " (comma then space) then attempts to remove them. However this hasn't worked and I have no idea what to try next.
Thanks
Chris
I am trying to print all the selected checkbox values to a textbox separated by commas (, ). However, the first value in the textbox always starts with a comma (, ) i.e. (, one, two, three) etc. My current script trys to check if the first 2 characters (substring) are ", " (comma then space) then attempts to remove them. However this hasn't worked and I have no idea what to try next.
<head>
<script type="text/javascript">
function list(c,n,z) {
s=document.form.marktext.value;
if (c.checked) {
if (s.indexOf(n)<0) s+=', '+n;
} else {
s=document.form.marktext.value.replace(', '+n,'');
}
z=", ";
if (s.substring(2) == z) s=s.substring(2);
document.form.marktext.value=s;}
</script>
</head>
<body>
<form name="form">
<input type="text" value="" name="marktext"><br>
<input type="checkbox" name="mark" onclick="list(this,'Word')">Word<br>
<input type="checkbox" name="mark" onclick="list(this,'Type')">Type<br>
<input type="checkbox" name="makr" onclick="list(this,'Other')">Other<br>
</form>
</body>Thanks
Chris
Last edited by xmassey : Nov 21st, 2007 at 3:12 am.
why not use a function like
run that on your onclick event and bobs your uncle
function getchecked() {
var newtxt = '';
var chkbx = document.getElementsByTagName('input');
for(var i = 0; i < chkbx.length; i ++) {
if(chkbx[i].type == 'checkbox' && chkbx[i].checked === true) {
if(newtxt.length !== 0) {
newtxt += ',';
}
newtxt += chkbx.innerHTML;
}
}
document.form.marktext.value = newtxt;
}run that on your onclick event and bobs your uncle
Last edited by Fungus1487 : Nov 21st, 2007 at 11:12 am.
When Autumn Falls [ http://www.whenautumnfalls.co.uk ] &&
Designdotworks [ http://www.designdotworks.co.uk ] Web / Graphic / Software Design
Designdotworks [ http://www.designdotworks.co.uk ] Web / Graphic / Software Design
it would be better to set a value for each checkbox instead of getting the innerHTML all the time
When Autumn Falls [ http://www.whenautumnfalls.co.uk ] &&
Designdotworks [ http://www.designdotworks.co.uk ] Web / Graphic / Software Design
Designdotworks [ http://www.designdotworks.co.uk ] Web / Graphic / Software Design
•
•
Join Date: Nov 2007
Posts: 4
Reputation:
Rep Power: 0
Solved Threads: 0
Hi, thanks for the replies,
I will try that code posted above. Thanks
The code is part of a webspace explorer system, each checkbox is given a value based on the path of the item being selected. Any number of checkboxes may be displayed all with a unique value. The javascript is so that the user is able to see which checkboxes have been selected in case a mistake is made. The HTML I provided was just an example, which I typed directly, sorry I made a spelling mistake.
Thanks again, Chris
I will try that code posted above. Thanks
The code is part of a webspace explorer system, each checkbox is given a value based on the path of the item being selected. Any number of checkboxes may be displayed all with a unique value. The javascript is so that the user is able to see which checkboxes have been selected in case a mistake is made. The HTML I provided was just an example, which I typed directly, sorry I made a spelling mistake.
Thanks again, Chris
Last edited by xmassey : Nov 24th, 2007 at 9:21 pm.
![]() |
•
•
•
•
•
•
•
•
DaniWeb JavaScript / DHTML / AJAX Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
- datagrid coantianing checkbox as column (ASP.NET)
- How to use HTML tags in PHP (PHP)
- Forms authorization, only want a few links (ASP.NET)
- Javascript, Form fields validation and submit (JavaScript / DHTML / AJAX)
- Printing a textbox? (C#)
- TextBox - how to determine focus (ASP.NET)
- Adding a checkbox column dynamically (C#)
- Forms Authorization/ Authentication using asp .net and vb .net (ASP.NET)
- Populating & Retrieving Data in a listbox : ASP.NET (w/ VB.NET) (ASP.NET)
Other Threads in the JavaScript / DHTML / AJAX Forum
- Previous Thread: radio button looses the selected state after validation
- Next Thread: People like Yaldex slow down the progress of web development



Linear Mode