This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes HTML, CSS and JavaScript and the fly likes How to setup a single checkbox to toggle / untoggle other check boxes? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "How to setup a single checkbox to toggle / untoggle other check boxes?" Watch "How to setup a single checkbox to toggle / untoggle other check boxes?" New topic
Author

How to setup a single checkbox to toggle / untoggle other check boxes?

James Dekker
Ranch Hand

Joined: Dec 09, 2006
Posts: 215
Have a dialog box (containing two divs):

(1) A <div> tag which contains an individual checkbox labeled as Select All.



(2) A <div> containing a reference to a bunch of check boxes (containing e-mail addresses).



JavaScript code:



What happens is that when I click on the Select All check box, it populates my To: text field with all the e-mail addresses. However, the checkboxes belonging inside emailCheckListId_ul are checked.

How can I set it up so that if I click on Select All, all the checkboxes are populated and when I uncheck on Select All, all the checks are removed from the check boxes and the e-mail addresses are removed from the To: text field?

Thanks for taking the time to read this...
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61456
    
  67

James Dekker wrote:How can I set it up so that if I click on Select All, all the checkboxes are populated and when I uncheck on Select All, all the checks are removed from the check boxes and the e-mail addresses are removed from the To: text field?


A change handler can be used to know when the state of a checkbox changes. The checked state of a checkbox can be changed by setting the checked to true or false (booleans, not strings). A field can be cleared by settings its value to the empty string.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
James Dekker
Ranch Hand

Joined: Dec 09, 2006
Posts: 215
Thanks for responding, Bear,

Yes, I know that I have to set checkbox.checked = true and text field = ""; but what I am asking is given the code I have, what should I do?

Please read the comments delimited by // in my code:



Would appreciate it if you could provide me with some sample code... Am I approaching this function the completely wrong way?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61456
    
  67

How do I obtain all the check boxes belonging to #emailCheckListId_ul and set the checked value to true?

I know you learned enough in your other post to be able to select the checkboxes. To set them as checked/unchecked, use .attr() to set the checked attribute to true or false.

And in case you haven't grasped it yet, I'm not going to write the code for you. You'll learn more by doing it yourself with some clues and help.

James Dekker
Ranch Hand

Joined: Dec 09, 2006
Posts: 215
Hi Bear,

I didn't mean to ask you to write it for me... Its just that I don't know much of JavaScript terminology or syntax so psuedocode was all I was seeking.

The reason I asked if my function's design is acceptable is because in a diffierent forum the responder told me that my method was convoluted.

From your previous post, it seems like my design is feasible...

Thanks again!
James Dekker
Ranch Hand

Joined: Dec 09, 2006
Posts: 215
Hi Bear,

I got it working in 5 minutes! Didn't use the "attr" method though.

Here's the complete solution:



Was hoping that there would be a shorter solution but this sure will suffice!

Thank you for all of your help!
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: How to setup a single checkbox to toggle / untoggle other check boxes?