View Full Version : checking checkboxes
AaronCampbell
04-13-2004, 04:33 PM
Ok, I have a form named order, and a set of checkboxes named board[]
<input name="board[]" type="checkbox" value="az_from_test" name="az_from_test" />From Arizona<br />
<input name="board[]" type="checkbox" value="nv_from_test" name="nv_from_test" />From Nevada<br />
<input name="board[]" type="checkbox" value="az_to_test" name="az_to_test" />To Arizona<br />
<input name="board[]" type="checkbox" value="nv_to_test" name="nv_to_test" />To Nevada<br />
I need to be able to check the boxes, but I can't seem to get it right. How would I make it check a certain box (either by name, OR by index 0-4)
Topher
04-13-2004, 04:58 PM
Not entirely sure whether you should have square brackets in a name, as these are used to refer to an index number of an element, but if you can:
Checkboxes shouldn't all have one name, otherwise you can't refer to them individually. How do you want to be able to check them? To check them by default at the start use:
<input name="board[]" type="checkbox" value="az_from_test" name="az_from_test" checked="checked" />From Arizona<br />
or try something along the lines of:
]<input name="board[]" type="checkbox" value="az_from_test" name="az_from_test" />From Arizona<br /> <a href='#' onClick='document.forms[0].board[].checked = true'>Clicky</a>
AaronCampbell
04-13-2004, 05:10 PM
The thing is, these are read by a php script as 1 array, holding the values of each checked box. That's why the names are all the same. Also, I can't check for each check box, as the form is dynamically created, and there is no way to tell how many check boxes there will be.
The code I gave above is messed up...each one has 2 names, and that was a slip up. here is how it really looks:
<input name="board[]" type="checkbox" value="az_from_test" />From Arizona<br />
<input name="board[]" type="checkbox" value="nv_from_test" />From Nevada<br />
<input name="board[]" type="checkbox" value="az_to_test" />To Arizona<br />
<input name="board[]" type="checkbox" value="nv_to_test" />To Nevada<br />
I tried all of the following:
document.order.board[].1.checked=true;
document.order.board[1].checked=true;
document.order.board[][1].checked=true;
But to no avail. Any other ideas?
EDIT:
I want to check them after the fact. The data that needs to be placed in the form is extracted from a database, and then the javascript (containing all the necessary info) is echo'd to the page, so that it will execute, and fill in the form. I just can't get the check boxes to work right.
Willy Duitt
04-13-2004, 05:36 PM
<script type="text/javascript">
<!--//
function checkAll(obj){
for(var i=0;i<obj.length;i++){
obj.ckb[i].checked = true;
}
}
function checkBush(obj){
for(var i=0;i<obj.length;i++){
obj[1].checked = true;
}
}
//-->
</script>
</HEAD>
<BODY>
<FORM NAME="myform">
<input type="checkbox" name="ckb" value="evedder@email.com">Eddie Vedder<br>
<input type="checkbox" name="ckb" value="gbush@email.com">George Bush<br>
<input type="checkbox" name="ckb" value="sgossard@email.com">Stone Gossard<br>
<input type="checkbox" name="ckb" value="mjordan@email.com">Michael Jordan<br>
<textarea name="txt1" cols="35" rows="15"></textarea><br>
<a href="javascript:checkAll(document.myform.ckb)">check all</a>
<a href="javascript:checkBush(document.myform.ckb)">check bush</a><br>
</form>
</body>
.....Willy
AaronCampbell
04-13-2004, 05:50 PM
Originally posted by AaronCampbell
The thing is, these are read by a php script as 1 array, holding the values of each checked box. That's why the names are all the same. Also, I can't check for each check box, as the form is dynamically created, and there is no way to tell how many check boxes there will be.
Without the []'s it does not see it as an array, instead it just sees it as the last value that was sent to it. (whatever the LAST box on the page that is checked is).
If I remove the []'s, I can set the check boxes, but then you can't tell what's checked, because all that is passed is the last value. If I leave them on though, I can't seem to set the check boxes. Anyone have any brilliant ideas?
Willy Duitt
04-13-2004, 07:21 PM
I'll look at it again later when I get some more time.
But I would think you should be able to access the checkboxes thru the tagName collection if you can not access them thru the name.
eg:
var inputs = document.forms[0].getElementsByTagName('input')
for(var i=0;i<inputs.length;i++) {
if(inputs.type == 'checkbox' && inputs[i].checked){
do this and that
}
.....Willy
AaronCampbell
04-13-2004, 08:39 PM
I think that your code is for checking the checkboxes, but I'm using php for checking everything. I could probably use that method to set the checkboxes though...I'll try when I get back to work tomorrow, and I'll let you know.
Vincent Puglia
04-13-2004, 09:09 PM
Hi AC & Willy,
If I remember right, there's a tutorial at my site ( http://members.aol.com/grassblad ) called Check Them All (the site may or may not still be there == aol is supposed to take it down)
Any rate, the following function does 2 things:
1) checks all -- regardless of name ; if you have two sets of boxes, you will need to add an additional test -- if (theElement.name == 'something')
2) inverses all -- especially useful if the set is huge and the user wants to check all but two or three.
<script type="text/javascript" language="javascript">
function checkAll(formObj, isInverse)
{
for ( i = 0; i < formObj.length; i++)
{
var theElement = formObj.elements[i];
if (theElement.type == 'checkbox')
if (!isInverse) theElement.checked = true;
else theElement.checked = (theElement.checked) ? 0 : 1;
}
}
</script>
<style type="text/css">
</style>
</head>
<body>
<FORM NAME="myform">
<input type="checkbox" name="ckb[]" value="1">1<br>
<input type="checkbox" name="ckb[]" value="2">2<br>
<input type="checkbox" name="ckb[]" value="3">3<br>
<input type="checkbox" name="ckb[]" value="4">4<br>
<input type="checkbox" name="ckb[]" value="1">1<br>
<input type="checkbox" name="ckb[]" value="2">2<br>
<input type="checkbox" name="ckb[]" value="3">3<br>
<input type="checkbox" name="ckb[]" value="4">4<br>
<input type="checkbox" name="ckb[]" value="1">1<br>
<input type="checkbox" name="ckb[]" value="2">2<br>
<input type="checkbox" name="ckb[]" value="3">3<br>
<input type="checkbox" name="ckb[]" value="4">4<br>
<a href="javascript:checkAll(document.myform, 0)">check all</a>
<a href="javascript:checkAll(document.myform, 1)">inverse all</a>
</form>
Vinny
Willy Duitt
04-14-2004, 01:13 AM
Heh Vinny;
That AhOLe site is still up?
I 'member reading somewhere that you moved.
And that your site would be taken down.
Anyways, I believe your method of going in thru the form.elements collection.
Is better than going in thru the DOM and accessing the TagName.
But, heh, whatever gets you the results you want.... ;)
.....Willy
Edit: Added 'purty colors. :D
Vincent Puglia
04-14-2004, 09:23 AM
Hi Willy,
Yeah, I discontinued the service over a month ago -- I'm on cable now, no site yet. Evidently, they haven't taken it down yet:
check them all but (http://members.aol.com/grassblad/html/chkAllBut.html)
Vinny
agent002
04-14-2004, 09:41 AM
Originally posted by Vincent Puglia
Hi Willy,
Yeah, I discontinued the service over a month ago -- I'm on cable now, no site yet. Evidently, they haven't taken it down yet:
check them all but (http://members.aol.com/grassblad/html/chkAllBut.html)
Vinny
Just BTW, my father still gets a newspaper delivered home, of which he cancelled the subscription over three years ago :eek:
Vincent Puglia
04-14-2004, 10:01 AM
Yeah, I know cause I'm getting the bill :)
Vinny
AaronCampbell
04-15-2004, 11:34 AM
The solution:
Well, since the checkboxes are created from lines in a config file, I used php to go through the file, and match the value that was passed to the file to the values from the file, and return the number corresponding to the box that needed to be checked (0-??).
Then I use this:
var j=0;
for(i=0;i<document.order.length;i++)
{
if(document.order.elements[i].type == 'checkbox')
{
if(5 == j || 4 == j)
{
document.order.elements[i].checked=true;
}
j++;
}
}
The red numbers are the box numbers that are echo'd out with php.
Since there can be no more than 2 boxes checked, it was pretty easy. If only 1 box is to be checked, the 2 red numbers are the same.
vBulletin® v3.6.7, Copyright ©2000-2009, Jelsoft Enterprises Ltd.