3 Replies - 920 Views - Last Post: 25 August 2011 - 06:30 AM

#1 polska03   User is offline

  • D.I.C Regular

Reputation: 5
  • View blog
  • Posts: 302
  • Joined: 28-November 09

checkall why dont work?

Posted 22 August 2011 - 05:03 PM

I want it to checkall the boxes, but it won't do it?
<html>
	<head>
		
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js">
</script>

</head>
<body>
	
<script type="text/javascript">
		
$(document).ready(function(){
function toggleChecked(status) {
$(".checkboxes input").each( function() {
$(this).attr("checked",status);
})
};
	
	
});
		
</script>
		
<input type="checkbox" onclick="toggleChecked(this.checked)"> Select / Deselect All	
	<div class="checkboxes">

<input type="checkbox" name="value[]"value="1">
<input type="checkbox" name="value[]"value="2">
<input type="checkbox" name="value[]" value="3">
				</div>
	
		
	

	</body>
</html>



This post has been edited by polska03: 22 August 2011 - 05:05 PM


Is This A Good Question/Topic? 0
  • +

Replies To: checkall why dont work?

#2 Jstall   User is offline

  • Lurker
  • member icon

Reputation: 434
  • View blog
  • Posts: 1,042
  • Joined: 08-March 09

Re: checkall why dont work?

Posted 23 August 2011 - 04:55 AM

Hi,

Try this slight modification:
<html>
	<head>
		
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js">
</script>

</head>
<body>
	
<script type="text/javascript">
		
$(document).ready(function(){
   $("#check_all").change(function(){
        $(".checkboxes input").attr("checked",$(this).attr("checked"));
   });
	
});
		
</script>
		
               <input type="checkbox" id = "check_all"> Select / Deselect All	
	       <div class="checkboxes">

                     <input type="checkbox" name="value[]"value="1">
                     <input type="checkbox" name="value[]"value="2">
                     <input type="checkbox" name="value[]" value="3">
		</div>

	</body>
</html>



It is untested but I think it will do the trick. Hope this helps :).
Was This Post Helpful? 0
  • +
  • -

#3 X-spert   User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 56
  • Joined: 25-August 11

Re: checkall why dont work?

Posted 25 August 2011 - 05:14 AM

Hi,

Jstall, I think your example is a good example but is not complete. It does not work for unclick ALL.
This is my correction.
<html>
	<head>
		
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js">
</script>

</head>
<body>
	
<script type="text/javascript">
		
$(document).ready(function(){
	$('#check_all').click(function(){
		$(".checkboxes input[type='checkbox']").each(function(){
        	if (this.checked == false) {
			this.checked = true;
		} else {
			this.checked = false;
		}
		});
	});
});
		
</script>
		
               <input type="checkbox" id = "check_all"> Select / Deselect All	
	       <div class="checkboxes">

                     <input type="checkbox" name="value[]" value="1">
                     <input type="checkbox" name="value[]" value="2">
                     <input type="checkbox" name="value[]" value="3">
		</div>

	</body>
</html>


Was This Post Helpful? 1
  • +
  • -

#4 Jstall   User is offline

  • Lurker
  • member icon

Reputation: 434
  • View blog
  • Posts: 1,042
  • Joined: 08-March 09

Re: checkall why dont work?

Posted 25 August 2011 - 06:30 AM

Hi,

You're right, that wasn't working. Strange, I know I have implemented a check/uncheck all type functionality using code like this on a number of occasions but it doesn't seem to be working, I'm at a loss as to why. Another way you could do it and avoid having to do the extra .each():
$("#check_all").change(function(){
	var checked = ($(this).attr('checked')) ? 'checked': false;
        $("div.checkboxes input[type=checkbox]").attr('checked',checked);
});



Thanks for the correction :).

This post has been edited by Jstall: 25 August 2011 - 06:31 AM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1