Get all checkboxes inside a div

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
klevis miho
Forum Contributor
Posts: 413
Joined: Wed Oct 29, 2008 2:59 pm
Location: Albania
Contact:

Get all checkboxes inside a div

Post by klevis miho »

I have this form

Code: Select all

<form name="myform" action="" method="post">
<div id="course1">
Bob<input type="checkbox" name="course[]"  value="bob" />
Charles<input type="checkbox" name="course[]"  value="charles" />
John<input type="checkbox" name="course[]"  value="john" />
<input type="button" name="check_all" value="Check All" onClick="CheckAll(document.getElementsByName('course[]'))" />
</div>
<input type="submit" />
</form>
and this JavaScript function that selects all checkboxes with the name "course[]":

Code: Select all

function CheckAll(chk)
{
for (i = 0; i < chk.length; i++)
chk[i].checked = true ;
}
This code works as expected, but my question is:
How can I check all the checkboxes which are inside the div with the id="course1"?

I will have more divs with id="course2", "course3" etc.

Any help would be appreciated.
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: Get all checkboxes inside a div

Post by josh »

in jquery
$('div.class checkbox')
klevis miho
Forum Contributor
Posts: 413
Joined: Wed Oct 29, 2008 2:59 pm
Location: Albania
Contact:

Re: Get all checkboxes inside a div

Post by klevis miho »

How to josh? I don't understand.
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: Get all checkboxes inside a div

Post by josh »

First "include" jquery on your page with a <script> tag

http://code.jquery.com/jquery-1.4.1.js

The $ is just an alias to the jquery() function. You can see I just pass it the CSS selector names of the elements I want it to find. The great thing about jquery is the code is so damn simple, and automatically works in modern browsers.
klevis miho
Forum Contributor
Posts: 413
Joined: Wed Oct 29, 2008 2:59 pm
Location: Albania
Contact:

Re: Get all checkboxes inside a div

Post by klevis miho »

Thnx josh, but I found a function that solved my problem:

Code: Select all

function checkByParent(aId, aChecked) {
    var collection = document.getElementById(aId).getElementsByTagName('INPUT');
    for (var x=0; x<collection.length; x++) {
        if (collection[x].type.toUpperCase()=='CHECKBOX')
            collection[x].checked = aChecked;
    }
}
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Get all checkboxes inside a div

Post by pickle »

I'm not sure getElementsByTagName() exists in all browsers.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
klevis miho
Forum Contributor
Posts: 413
Joined: Wed Oct 29, 2008 2:59 pm
Location: Albania
Contact:

Re: Get all checkboxes inside a div

Post by klevis miho »

I tried it in chrome, firefox and IE6, and it works
Post Reply