The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Web Design
> JavaScript Development
|
Uncheck a checkbox if another checkbox is checked
Discuss Uncheck a checkbox if another checkbox is checked in the JavaScript Development forum on Dev Shed. Uncheck a checkbox if another checkbox is checked JavaScript Development forum discussing JavaScript and DHTML, AJAX, and issues such as coding cross-browser JavaScript.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

October 21st, 2012, 03:36 PM
|
|
Registered User
|
|
Join Date: Oct 2012
Posts: 23
Time spent in forums: 4 h 47 m 16 sec
Reputation Power: 0
|
|
|
Uncheck a checkbox if another checkbox is checked
I'm new to coding, but I'm getting through every problem I've encountered thanks to the people here and on other forums.
Now I'm wondering:
- I have to 2 different arrays of check boxes (150 boxes each)
- when a user clicks a card-shuffle button, it builds a deck based on the status of array #2
- checkbox #1 in array #1 corrosponds to checkbox #1 in array #2.
I need a statement that says " if checkbox #1 in array #1 ISN'T checked, then uncheck checkbox #1 in array #2."
And so on and so forth for the next checkboxes in the arrays.
Is this possible?
Here's what I've been trying: (maybe I'm accessing the form elements incorrectly? I don't get any errors from this code, but it doesn't work)
Code:
function unflip_cards(){
if (form.item_1_array_1.checked == true)
form.item_1_array_2.checked == true;
if (form.item_2_array_1.checked == true)
form.item_2_array_2.checked == true;
if (form.item_3_array_1.checked == true)
form.item_3_array_2.checked == true;
if (form.item_4_array_1.checked == true)
form.item_4_array_2.checked == true;
if (form.item_5_array_1.checked == true)
form.item_5_array_2.checked == true;
if (form.item_6_array_1.checked == true)
form.item_6_array_2.checked == true;
if (form.item_7_array_1.checked == true)
form.item_7_array_2.checked == true;
if (form.item_8_array_1.checked == true)
form.item_8_array_2.checked == true;
}
|

October 21st, 2012, 04:25 PM
|
|
|
That is going to be a lot of lines of code. Programming and computers were invented to save us time
'==' is a comparison operator and '=' is an assignment operator.
Try the following code. Class name of 'card' to your 'array 1' check boxes.
JavaScript Code:
Original
- JavaScript Code |
|
|
|
function limitChecks(obj) { if(obj.checked == false) { document.forms['FORM NAME'].elements[obj.name+'_a'].checked = false; } } onload = function() { var cards = document.getElementsByTagName('input'); for (var i=0,x=cards.length; i<x; i++) { if (cards[i].className == 'card') { cards[i].onclick = function() { limitChecks(this) } } } }
HTML4Strict Code:
Original
- HTML4Strict Code |
|
|
|
<input type="checkbox" name="card_1" class="card"> <input type="checkbox" name="card_1_a">
|

October 22nd, 2012, 12:04 PM
|
|
Registered User
|
|
Join Date: Oct 2012
Posts: 23
Time spent in forums: 4 h 47 m 16 sec
Reputation Power: 0
|
|
Quote: | Originally Posted by Winters That is going to be a lot of lines of code. Programming and computers were invented to save us time
'==' is a comparison operator and '=' is an assignment operator.
Try the following code. Class name of 'card' to your 'array 1' check boxes.
JavaScript Code:
Original
- JavaScript Code |
|
|
|
function limitChecks(obj) { if(obj.checked == false) { document.forms['FORM NAME'].elements[obj.name+'_a'].checked = false; } } onload = function() { var cards = document.getElementsByTagName('input'); for (var i=0,x=cards.length; i<x; i++) { if (cards[i].className == 'card') { cards[i].onclick = function() { limitChecks(this) } } } }
HTML4Strict Code:
Original
- HTML4Strict Code |
|
|
|
<input type="checkbox" name="card_1" class="card"> <input type="checkbox" name="card_1_a">
|
Thanks there mod. Your complicated script wasn't the solution. Figured it all out now, I now have the perfect deck of cards code. It handles all 3 decks of 150 cards each PERFECTLY. And 135 of 150 cards from each deck are 'collectable' but my code even handles all this perfectly.
My javascript skills aren't very good, but after figuring out this mess, my knowledge of JS is that much greater.
Thanks again though for trying to help.
|

October 22nd, 2012, 12:12 PM
|
|
|
|
I'm glad you have it working, however the script posted did exactly what you asked for. If you required more complexity, you needed to specify.
|

October 22nd, 2012, 02:10 PM
|
|
Registered User
|
|
Join Date: Oct 2012
Posts: 23
Time spent in forums: 4 h 47 m 16 sec
Reputation Power: 0
|
|
Quote: | Originally Posted by Winters I'm glad you have it working, however the script posted did exactly what you asked for. If you required more complexity, you needed to specify. |
No, actually I needed a LESS complicated script lol. I'm still a rookie coder in a few ways.
Cheers 
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|