www.webdeveloper.com
Results 1 to 8 of 8

Thread: check/uncheck boxes

  1. #1
    Join Date
    Apr 2014
    Posts
    6

    check/uncheck boxes

    I have two check boxes, 'a1' and 'p1'. I simply need a script that will either have nothing checked or only 'a1' or only 'p1' checked.

    I was using(vice versa for the other checkbox):

    if(this.getField("a1").isBoxChecked(0))
    {
    this.getField("p1").checkThisBox(0, false);
    }

    and this worked fine, on the PC, however when using the IPad, this script would not function.

  2. #2
    Join Date
    Nov 2010
    Posts
    1,084
    are 'a1' and 'p1' names or IDs?

  3. #3
    Join Date
    Apr 2014
    Posts
    6
    IDs

  4. #4
    Join Date
    Apr 2014
    Posts
    6
    sorry, 'a1' is the name i have assigned the checkbox

  5. #5
    Join Date
    Nov 2010
    Posts
    1,084
    assuming that they are both names, and that they are the first elements on the page with those names, and that in the case of both being checked you only want a1 to remain checked...
    Code:
    <body>
    <input type="checkbox" name ="a1"/>
    <input type="checkbox" name ="p1"/>
    <input type="button" onclick ="checkIt()" value = "check boxes"/>
    <script type="text/javascript">
    function checkIt(){
    var cb1 =document.querySelector('input[name="a1"]');
    var cb2 =document.querySelector('input[name="p1"]');
    
    if(cb1.checked){
    cb2.checked=false;
    }
    
    }
    </script>
    </body>

  6. #6
    Join Date
    Oct 2013
    Posts
    486
    Quote Originally Posted by coolhandbr View Post
    I simply need a script that will either have nothing checked or only 'a1' or only 'p1' checked.
    I don't understand why you are trying to make checkboxes act like radio buttons. The quoted description of the behavior you are trying to script is exactly why radio buttons exist. Example:

    HTML Code:
    <input type="radio" name="choices" id="a1" value="choice 1"> <label for="a1">Choice 1</label><br>
    <input type="radio" name="choices" id="p1" value="choice 2"> <label for="p1">Choice 2</label><br>
    <input type="radio" name="choices" id="clearChoices" value=""> <label for="clearChoices">None</label> <!-- Optional. Submits nothing. Exists to let the user change his/her mind :) -->
    No scripting required and it works cross-browser and cross-device. You can, however, script the buttons to do other things if you have that need.
    Last edited by Kevin2; 07-02-2014 at 04:56 PM. Reason: oops fix

  7. #7
    Join Date
    May 2014
    Posts
    915
    I'm with Kevin2 -- first thing I was thinking was "why not just use radio buttons" since they already do what you're trying to waste JS doing.
    Java is to JavaScript as Ham is to Hamburger.

  8. #8
    Join Date
    Mar 2007
    Location
    localhost
    Posts
    2,348
    With Kevin and deathshadow on this, radio is the way to go.

    Only thing I can think of is if you are going to have multiple check boxes with various combos to select and not all options will be available depending on what is checked... Even then you can use multiple radio sets and have those selections of sub sets determined on the main set choice.
    Yes, I know I'm about as subtle as being hit by a bus..(\\.\ Aug08)
    Yep... I say it like I see it, even if it is like a baseball bat in the nutz... (\\.\ Aug08)
    I want to leave this world the same way I came into it, Screaming, Incontinent & No memory!
    I laughed that hard I burst my colostomy bag... (\\.\ May03)
    Life for some is like a car accident... Mine is like a motorway pile up...

    Problems with Vista? :: Getting Cryptic wid it. :: The 'C' word! :: Whois?

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles