Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Dec 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Using buttons to check and uncheck checkboxes

    I'm trying to write some code that uses buttons to check and uncheck certain checkboxes. I am stumped and all the code I have been finding is using for loops to check or uncheck all the boxes, when I just want to check or uncheck certain ones.

    Here's an example of the type of function I'm trying to use:
    Code:
     function checkSome (form) {
    	document.myForm.list.one.checked = true;
    	document.myForm.list.two.checked = true;
    	document.myForm.list.three.checked = false;
    }
    And the button:
    Code:
    <input type="button" value="Option 1" onClick="checkSome(this.form)"/>

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,869
    Thanks
    79
    Thanked 4,421 Times in 4,386 Posts
    What was "list." in there for???
    Code:
    function checkSome (form) {
    	form.one.checked = true;
    	form.two.checked = true;
    	form.three.checked = false;
    }
    Assuming, that is, that you had
    Code:
        <input type="checkbox" name="one" />
        <input type="checkbox" name="two" />
        <input type="checkbox" name="three" />
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #3
    New to the CF scene
    Join Date
    Dec 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    The form I am using has several different inputs (not just the checkboxes) so I have
    Code:
    <input type="checkbox" name="list" value="one"/>
    <input type="checkbox" name="list" value="two"/>
    <input type="checkbox" name="list" value="three"/>
    From my understanding I would have to have:
    Code:
    function checkSome (form) {
    	form.list.one.checked = true;
    	form.list.two.checked = true;
    	form.list.three.checked = false;
    }
    But it's still not working.

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,079
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    You are confusing the value of the checkbox with its name and index.

    Code:
    function checkSome (form) {
    form.list[0].checked = true;
    form.list[1].checked = true;
    form.list[2].checked = false;
    }
    Remember that the index starts at 0 (the first checkbox in the group list is list[0]).


    Obama can't be himself because of who he is. But now he is who he is. - Political commentator, BBC 2.


  •  

    Posting Permissions

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