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
    L.U.N.E for anyone and everyone who has an interest in literature

  2. #2
    Join Date
    May 2006
    Location
    Somewhere behind your screen
    Posts
    1,648
    Code:
    function checkAll(field) {
    	for (i = 0; i < field.length; i++)
    	field[i].checked = (field[i].checked== true)?false:true;
    }
    xxx: Guess Buddhist riddle: "What is the sound of one hand clapping?"
    yyy: facepalm

  3. #3
    Join Date
    Jan 2010
    Posts
    127
    Works perfect, thank you.
    L.U.N.E for anyone and everyone who has an interest in literature

  4. #4
    Join Date
    May 2006
    Location
    Somewhere behind your screen
    Posts
    1,648
    you welcome )
    xxx: Guess Buddhist riddle: "What is the sound of one hand clapping?"
    yyy: facepalm

  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 )"/>
    Where used, return should be executed unconditionally and always as the last statement in the function.

    That's my signature, it's not part of the damn post!

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