Ady Wicaksono Daily Activities

create “select all” & “deselect all” for checkbox option with javascript

with one comment

Sometimes we create HTML document that contains some checkbox option.Sometime we need to click every checkbox option, so is there quick way for doing this? Off course we can do it easily if there’s “select all” button to click every checkbox option…, so how to do it?

Here’s the example code for doing this

function checkAll(){
	var i = document.f['keycode_id[]'].length;
	if(i == undefined){
		document.f['keycode_id[]'].checked = true;
	}else{
		for(j = 0; j < i; j++){
			document.f['keycode_id[]'][j].checked = true;
		}
	}
}	

function unCheckAll(){
	var i = document.f['keycode_id[]'].length;
	if(i == undefined){
		document.f['keycode_id[]'].checked = false;
	}else{
		for(j = 0; j < i; j++){
			document.f['keycode_id[]'][j].checked = false;
		}
	}
}

With assumption that the document form is “f” and the option chekbox variable name is “keycode_id[]”, otherwise you only need to change it. Next step youonly need to create button with event “onClick” will execute one of those 2 function (select all or deselect all)

Written by adywicaksono

October 31, 2007 at 2:13 pm

Posted in javascript

One Response

Subscribe to comments with RSS.

  1. Amazing worked right for me thank you

    Fernando

    April 19, 2010 at 4:42 pm


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: