37 users online (0 members and 37 guests)  


  Results 1 to 3 of 3

Related

  1. Replies: 0
  2. Form - checkboxes & file upload    Forum: PHP Forum
    Replies: 1
  3. Checkboxes and arrays    Forum: Javascript Forum
    Replies: 18
  4. php lists and checkboxes    Forum: PHP Forum
    Replies: 2
  1. #1
    SeventhSon's Avatar
    New User

    Status
    Offline
    Join Date
    Oct 2006
    Posts
    9

    Buttons and checkboxes

    I found a script for a button and checkbox, but can't get it to do what I want when adding more buttons and checkboxes. I'm wanting to click a button and have the checkbox next to that button to check. I'm missing something in the script. Can you help?

    Code:
    <html>
    <head>
    <script type="text/javascript">
    function clickCheckbox()
      {
      document.getElementById('check1').click()
      document.getElementById('check2').click()
      }
    </script>
    </head>
    <body>
    
    <form>
    <input type="checkbox" id="check1" />
    <input type="button" onclick="clickCheckbox()" value="Box 1" />
    <br />
    <input type="checkbox" id="check2" />
    <input type="button" onclick="clickCheckbox()" value="Box 2" />
    </form>
    
    </body>
    </html>

  2. #2
    ALL's Avatar
    Super Dooper Nerd

    Status
    Offline
    Join Date
    Feb 2005
    Location
    localhost
    Posts
    382

    Re: Buttons and checkboxes

    hmmm.... where to start....

    first off you should provide all the code when you are asking a question....


    second... you are trying to activate the checkboxes? because that looks like what you are trying to do.

    I seggest posting all the code and try explaining what you want in more detail.

    -ALL

  3. #3
    coothead's Avatar
    bald headed old fart

    Status
    Offline
    Join Date
    Aug 2003
    Location
    chertsey, a small town 25 miles south west of london, england.
    Posts
    732

    Re: Buttons and checkboxes


    Hi there SeventhSon,

    try it like this...
    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
       "http://www.w3.org/TR/html4/strict.dtd">
    <html>
    <head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    
    <script type="text/javascript">
    <!--
    
    window.onload=function() {
       inp=document.getElementsByTagName('input');
    for(c=0;c<inp.length;c++) {
    if(inp[c].type=='button') {
       inp[c].onclick=function() {
       id='check'+this.value.split('box ')[1];
       obj=document.getElementById(id);
       obj.checked=(obj.checked==true)?false:true;
        }
       }
      }
     }
    //-->
    </script>
    
    </head>
    <body>
    
    <form action="#">
    <div>
    <input type="checkbox" id="check1" />
    <input type="button" value="box 1" />
    <br />
    <input type="checkbox" id="check2" />
    <input type="button" value="box 2" />
    <br />
    <input type="checkbox" id="check3" />
    <input type="button" value="box 3" />
    <br />
    <input type="checkbox" id="check4" />
    <input type="button" value="box 4" />
    </div>
    </form>
    
    </body>
    </html>
    ...also note that I have provided the option of unchecking the checkbox with another click of the button.



Tags for this Thread