1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Check All- Check None script for array of checkboxes

Discussion in 'JavaScript' started by computerzworld, Jan 17, 2008.

  1. #1
    Hello,
    I have an array of checkbox as mycheckbox[]. How can I make all these checkboxes checked and unchecked using clicking on link/button? Please help me. Thanks in advance.
     
    computerzworld, Jan 17, 2008 IP
  2. rkquest

    rkquest Well-Known Member

    Messages:
    828
    Likes Received:
    23
    Best Answers:
    0
    Trophy Points:
    140
    #2
    Try this one..

    
    <script>
    function select(a) {
    	var theForm = document.myForm;
    	for (i=0; i<theForm.elements.length; i++) {
    		if (theForm.elements[i].name=='mycheckbox[]')
    			theForm.elements[i].checked = a;
    	}
    }
    </script>
    
    <form name="myForm">
    <input type="checkbox" name="mycheckbox[]" value="somevalue1" /><br/>
    <input type="checkbox" name="mycheckbox[]" value="somevalue2" /><br/>
    <input type="checkbox" name="mycheckbox[]" value="somevalue3" />
    </form>
    
    <a href="javascript:select(1)">Check all</a> |
    <a href="javascript:select(0)">Uncheck all</a> 
    
    HTML:
     
    rkquest, Jan 17, 2008 IP