Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 260
  • Last Modified:

Select all checkboxes on a page in a specific section

I have a site where on it is a range of checkboxes. All have different names and values, and are housed in a single table.
I am after a script that will allow me to select all checkboxes in this range without interfering with the others on the same page if possible. So far it's been all on the page or nothing...
Maybe if it could run by the Table name/ID or if I set all checkboxes to have the same ID, it could help...

Code samples/examples would be appreciated.
0
kiwistag
Asked:
kiwistag
  • 3
1 Solution
 
Tom BeckCommented:
You have not included the jQuery topic area. Is jQuery not an option?

Giving all the checkboxes the same id is NOT an option, but you could give them the same name or the same class and access them that way.

Or, if they all appear in a specific column of a table you can access them like:
var allRows = getElementById('tableId').getElementsByTagName('tr');
for (i = 0; i < allRows.length; i++) {
    var cells = allRows[i].getElementsByTagName('td');
   //cells[number] would be the cell with the checkbox in it, set it to checked
}

Open in new window

0
 
Tom BeckCommented:
Of course with jQuery it's simple, regardless of where the checkboxes appear in the table or on the page for that matter. Just give them all the same class name.

http://jsfiddle.net/tTF3k/2/
0
 
kiwistagAuthor Commented:
Sorry, forgot to include jQuery. Yes! That worked perfectly - thank you :)
0
 
Tom BeckCommented:
You're welcome. Thanks for the points.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now