Jump to content

select all checkboxes script


Destramic

Recommended Posts

hey guys im trying to make a select all checkboxes (grouped) script...my problem im having is that

 

var group_checked_count = $('group:checked').length;
var group_count         = $(group).length;

 

isnt returning the correct value  as they both are returning 0

var group_checked_count should return 1...and

var group_count should return 4

 

it seems im doing something wrong...if anyone could point out what that is please

 

<!DOCTYPE html>
<html>
<head>
  <script type="text/javascript" src="/ajax/jquery/libary/jquery.js"></script>
  <script>	
function toggleCheckboxes(group, element) {

group   = $('input[name='+ group +']');
element = $('#'+ element +'');

var group_checked_count = $('group:checked').length;
var group_count         = $(group).length;

if ($(element).text() == '')
{
	if ($(group_count) / $(group_checked_count) < 1) {
		$(element).text() = 'Select All';
	}
	else{
		$(element).text() = 'Deselect All';
	}
}
}

toggleCheckboxes('sports', 'selectbutton');
</script>
</head>
<body>
<form>
<span id="selectbutton"></span>

Soccer: <input type="checkbox" name="sports" value="soccer"  /><br />
Football: <input type="checkbox" name="sports" value="football" checked /><br />
Baseball: <input type="checkbox" name="sports" value="baseball"  /><br />
Basketball: <input type="checkbox" name="sports" value="basketball"  />
</form>

</body>
</html>

Link to comment
Share on other sites

var input_tag=document.getElementsByTagName("input");

 

for (i=0; i<input_tag.length; i++){

 

if(input_tag.type==='checkbox'){input_tag.checked=true;}

 

}

 

// Sorry i couldnt really go though your script and fix it.  But i gave you a simple select all checkboxes option above.

Link to comment
Share on other sites

function toggleCheckboxes(g, e) {
var element = $('#'+e);
var groupCount = $('input[name='+g+']').length;
var groupCheckedCount = $('input[name='+g+']').is(':checked').length;

if (element.text().length == 0) {
	if ((groupCount/groupCheckedCount) < 1) {
		element.text('Select All');
	} else{
		element.text('Deselect All');
	}
}
}

function selectAll(g, e) {
$('input[name=sports]').each(function(i) {
	$(this).attr('checked', true);
});

$('#'+e).text('Deselect All');
}

function deselectAll(g, e) {
$('input[name=sports]').each(function(i) {
	$(this).removeAttr('checked');
});

$('#'+e).text('Select All');
}

$(document).ready(function() {
toggleCheckboxes('sports', 'selectbutton');

$('#selectbutton').bind('click', function() {
	var text = $(this).text();

	if (text == 'Select All') {
		selectAll('sports', 'selectbutton');
	} else {
		deselectAll('sports', 'selectbutton');
	}
});
});

 

Let me bring to your attention what you were doing wrong according to:

var group_checked_count = $('group:checked').length;
var group_count = $(group).length;

 

In the variable group_checked_count, you're trying to explicitly look for the selector "group". This is not looking for the group variable, but looking for group as it would look for input. Also, you're passing a parameter in as group and then setting a variable named group. You reset the group parameter to the value of the group variable to "$('input[name=+ group +]');". So when you go to set group_checked_count, you're really trying to set the variable to $('$('input[name=+ group +]');:checked').length;. Because of this same reason, the variable group_count doesn't work properly. There were a few more errors in your code, but I won't elaborate on them. Take a look at the code snippet I supplied you and let me know if you have any questions.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.