www.webdeveloper.com
Results 1 to 8 of 8

Thread: [RESOLVED] 1 checkbox disables the rest of the checkboxes

  1. #1
    Join Date
    Jan 2012
    Posts
    34

    resolved [RESOLVED] 1 checkbox disables the rest of the checkboxes

    ive got 1 checkbox called "selectall" and i want it to disable the rest of the checkboxes in the form. if i give it an id of "selall" and all of the rest id's of "all" when i click it only the 1st gets disabled using
    Code:
    function assgAll(){
    	if (document.getElementById('selall').checked){document.getElementById('all').disabled=true;}
    	else {document.getElementById('all').disabled=false;}

  2. #2
    Join Date
    Mar 2009
    Posts
    521
    Normally you shouldn't give more than one element on a page the same id. The "getElementById" method will only return the first element with that id. You can do it, but its considered bad form.

    But sometimes you may need to do it as in the case where someone else has written the HTML and you are not allowed to alter it. Here's how:

    Code:
    //get an array containing all references with a like id
    
    function multiId(id){
    var x, i, len, h;
    h = [];
    do{
        x = document.getElementById(id);
        if(x !== null){
            h.push(x);
            x.id = undefined;
         }
        }while(x !== null);
    //now put the id's back
    len = h.length;
    for(i = 0; i < len; i++){
        h[i].id = id;
    }
    return h;
    }
    
    
    //at this point you can use the return value (an array) to loop through the elements
    
    var z = multiId('myId');
    
    for(var i =0; i < z.length; i++){
          z[i].disabled = true;
    }

  3. #3
    Join Date
    Jan 2012
    Posts
    34
    i went with this
    Code:
    function assgAll(){
    	if (document.getElementById('selall').checked){
    		for (i = 0; i < document.myform.ckboxes.length; i++){
    		document.myform.ckboxes[i].disabled = true ;}
    		document.getElementById('selall').disabled = false;
    		}
    	else {
    		for (i = 0; i < document.myform.ckboxes.length; i++){
    		document.myform.ckboxes[i].disabled = false ;}
    		}	
    }
    problem now is that it worked untill i set the checkboxes up as an array
    HTML Code:
    <form name="myform" action="" method="post">
    <input id="selall" type='checkbox' onclick="assgAll()" />
    <input name='ckboxes[]' type='checkbox' value='1' />
    <input name='ckboxes[]' type='checkbox' value='2' />
    <input name='ckboxes[]' type='checkbox' value='3' />
    </form
    Last edited by Da_Elf; 05-02-2013 at 12:33 AM.

  4. #4
    Join Date
    May 2006
    Location
    Somewhere behind your screen
    Posts
    1,672
    here is a code sample for show/hide/check/uncheck etc. try that one

  5. #5
    Join Date
    Jan 2012
    Posts
    34
    thanks but thats jquery

  6. #6
    Join Date
    May 2006
    Location
    Somewhere behind your screen
    Posts
    1,672
    jquery is a deadly sin or what?

  7. #7
    Join Date
    Jan 2012
    Posts
    34
    i dont know jquery.

  8. #8
    Join Date
    Jan 2012
    Posts
    34

    resolved

    Code:
    function assgAll(){
    	var chGo=document.getElementsByName("ckboxes[]");
    	if (document.getElementById('selAll').checked){
    		for (i = 0; i < chGo.length; i++){
    		chGo[i].checked = true ;}
    		}
    	else {
    		for (i = 0; i < chGo.length; i++){
    		chGo[i].checked = false ;}
    		}	
    }
    this worked.

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