#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    23
    Rep 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;
    
    }
  2. #2
  3. No Profile Picture
    Super Moderator
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jul 2003
    Posts
    4,009
    Rep Power
    2791
    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:
    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:
    	<input type="checkbox" name="card_1" class="card">
     
     
    	<input type="checkbox" name="card_1_a">
    [PHP] | [Perl] | [Python] | [Java] != [JavaScript] | [XML] | [C] | [C++] | [LUA] | [MySQL] | [FirebirdSQL] | [PostgreSQL] | [HTML] | [XHTML] | [CSS]

    W3Fools - A W3Schools Intervention.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    23
    Rep Power
    0
    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:
    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:
    	<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.
  6. #4
  7. No Profile Picture
    Super Moderator
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jul 2003
    Posts
    4,009
    Rep Power
    2791
    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.
    [PHP] | [Perl] | [Python] | [Java] != [JavaScript] | [XML] | [C] | [C++] | [LUA] | [MySQL] | [FirebirdSQL] | [PostgreSQL] | [HTML] | [XHTML] | [CSS]

    W3Fools - A W3Schools Intervention.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    23
    Rep Power
    0
    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

IMN logo majestic logo threadwatch logo seochat tools logo