SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Wizard WorldNews's Avatar
    Join Date
    Nov 2007
    Posts
    1,033
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    How does one uncheck a check Box with Javacript?

    Hello,

    How does one uncheck a check Box with Javacript when the rows of the Table are dynamically created by
    Php so we do not know what the row numbers are going to be before hand.

    That is we have these 2 check boxes:

    <input type="checkbox" name="approve[]" value="<?php echo $user_id; ?>" checked onClick="uncheck(reject, <?php echo ($row - 1); ?>>
    &
    <input type="checkbox" name="reject[]" value="<?php echo $user_id; ?>" onClick="uncheck(approve, <?php echo ($row - 1); ?>">

    So if they click on check box "reject", we then want check box "approve" unchecked and vis-versa.

    FYI, I was using this Javascript code:

    function uncheck(field, i)
    {
    field[i].checked = false ;
    }


    But the combination of above is not working!

    ThanX,

    Anoox search engine volunteer

    www.anoox.com

  2. #2
    Non-Member
    Join Date
    Apr 2011
    Location
    no fixed address
    Posts
    851
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sounds like you should be using radio buttons instead of check boxes.

  3. #3
    SitePoint Wizard WorldNews's Avatar
    Join Date
    Nov 2007
    Posts
    1,033
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Well the reason I need to use Checkboxes is that I need the values selected in all the various rows added to an array, which then I can loop through, that is what check box as an array allows but radio which holds a single value does not.

    Or are you saying that there is a form of a radio which allows for collecting the selected values in an array, that is:

    In check box we have:

    <input type="checkbox" name="reject[]" value="<?php echo $user_id; ?>">

    is the same possible in a radio like:

    <input type="radio" name="reject[]" value="<?php echo $user_id; ?>">
    whereby then reject[] would be an array of values filled by radio?



    Quote Originally Posted by webdev1958 View Post
    Sounds like you should be using radio buttons instead of check boxes.

    Anoox search engine volunteer

    www.anoox.com

  4. #4
    Non-Member
    Join Date
    Apr 2011
    Location
    no fixed address
    Posts
    851
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    So if they click on check box "reject", we then want check box "approve" unchecked and vis-versa.
    Then why do you want to uncheck another check box when one is clicked if you want all the selected values?

    You're posting only bits and pieces of your code and it's not consistent with what you say you want to do.

    Hopefully someone else will come along to show you what you have done incorrectly.

  5. #5
    SitePoint Wizard WorldNews's Avatar
    Join Date
    Nov 2007
    Posts
    1,033
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Ok, no problem. Thanks anyway.

    Quote Originally Posted by webdev1958 View Post
    Then why do you want to uncheck another check box when one is clicked if you want all the selected values?
    You're posting only bits and pieces of your code and it's not consistent with what you say you want to do.

    Hopefully someone else will come along to show you what you have done incorrectly.

    Anoox search engine volunteer

    www.anoox.com

  6. #6
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,036
    Mentioned
    187 Post(s)
    Tagged
    2 Thread(s)
    Try

    checked = null;

  7. #7
    SitePoint Guru
    Join Date
    Sep 2006
    Posts
    731
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by WorldNews View Post
    <input type="checkbox" name="reject[]" value="<?php echo $user_id; ?>" onClick="uncheck(approve, <?php echo ($row - 1); ?>" ) >
    Looks like you're missing the ) after the parameter. Use the error console.

    Does this do what you need?
    Code:
    <!DOCTYPE HTML>
    <html>
    <head>
    <script type="text/javascript">
    
    function uncheck( group )
    {
      if( group.length )
      {
       for( var n = 0; group[ n ]; n++ )
        group[ n ].checked = false;
      }
      else
       group.checked = false ;
    }
    
    </script>
    
    </head>
    <body>
    <p>
    <form action='#'>
     <input type="checkbox" name="approve[]" value="XXX" checked onClick="uncheck(this.form[ 'reject[]' ] )">
    
     <input type="checkbox" name="reject[]" value="YYY" onClick="uncheck(this.form[ 'approve[]' ] )">
     <input type="checkbox" name="reject[]" value="ZZZ" onClick="uncheck(this.form[ 'approve[]' ] )">
    </form>
    </body>
    </html>
    Tab-indentation is a crime against humanity.


Bookmarks

Posting Permissions

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