User Name Password Register
DaniWeb IT Discussion Community
All
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
Reply
Join Date: Nov 2007
Posts: 4
Reputation: xmassey is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
xmassey xmassey is offline Offline
Newbie Poster

Checkbox value appended to textbox

  #1  
Nov 21st, 2007
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.

<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.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Apr 2007
Location: Birmingham
Posts: 378
Reputation: Fungus1487 is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 38
Fungus1487's Avatar
Fungus1487 Fungus1487 is offline Offline
Posting Whiz

Re: Checkbox value appended to textbox

  #2  
Nov 21st, 2007
why not use a function like

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
Reply With Quote  
Join Date: Apr 2007
Location: Birmingham
Posts: 378
Reputation: Fungus1487 is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 38
Fungus1487's Avatar
Fungus1487 Fungus1487 is offline Offline
Posting Whiz

Re: Checkbox value appended to textbox

  #3  
Nov 21st, 2007
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
Reply With Quote  
Join Date: Jan 2007
Posts: 2,604
Reputation: MidiMagic is on a distinguished road 
Rep Power: 7
Solved Threads: 119
MidiMagic's Avatar
MidiMagic MidiMagic is offline Offline
Posting Maven

Re: Checkbox value appended to textbox

  #4  
Nov 23rd, 2007
I see a spelling error in the name of the third checkbox.
Daylight-saving time uses more gasoline
Reply With Quote  
Join Date: Nov 2007
Posts: 4
Reputation: xmassey is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
xmassey xmassey is offline Offline
Newbie Poster

Re: Checkbox value appended to textbox

  #5  
Nov 24th, 2007
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
Last edited by xmassey : Nov 24th, 2007 at 9:21 pm.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb JavaScript / DHTML / AJAX Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the JavaScript / DHTML / AJAX Forum

All times are GMT -4. The time now is 9:02 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC