www.webdeveloper.com
Results 1 to 5 of 5

Thread: Select / Deselect checkbox

  1. #1
    Join Date
    Jan 2010
    Posts
    127

    Select / Deselect checkbox

    I've been working on a select / deselect which uses a dynamically generated number of checkboxes. These checkboxes have an array as a name which some php script I have uses.

    I found a solution to selecting them all here: http://www.webdeveloper.com/forum/sh...d.php?t=176011

    The above solution was:
    Code:
    function checkAll(field) {
    	for (i = 0; i < field.length; i++)
    	field[i].checked = true;
    }
    And I echo this later on:
    Code:
    <input type="checkbox" name="delallchk" onclick="checkAll(document.form1[\'deleteall[]\'])"/>
    I was wondering if someone could point me in the right direction as to how I'd change the select all into a select / deselect?

    Thanks in advance,

    Aravona

  2. #2
    Join Date
    May 2006
    Location
    Somewhere behind your screen
    Posts
    1,674
    Code:
    function checkAll(field) {
    	for (i = 0; i < field.length; i++)
    	field[i].checked = (field[i].checked== true)?false:true;
    }

  3. #3
    Join Date
    Jan 2010
    Posts
    127
    Works perfect, thank you.

  4. #4
    Join Date
    May 2006
    Location
    Somewhere behind your screen
    Posts
    1,674
    you welcome )

  5. #5
    Join Date
    Mar 2007
    Location
    U.K.
    Posts
    1,127
    Quote Originally Posted by Aravona View Post
    I was wondering if someone could point me in the right direction as to how I'd change the select all into a select / deselect?
    Padonak's code will act as an 'invert all'. If you wanted a select all/deselect all:

    Code:
    function checkAll( field, status ) 
    {     
      for (var i = 0; i < field.length; i++)     
       field[i].checked = status; 
    }      
    
    <input type="checkbox" name="delallchk" onclick="checkAll( document.form1[\'deleteall[]\'], this.checked )"/>

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