SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Wizard gold trophysilver trophybronze trophy dc dalton's Avatar
    Join Date
    Nov 2004
    Location
    Right behind you, watching, always watching.
    Posts
    5,431
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    checking checkboxes - jQuery

    Been through a million blogs, examples etc and I can't for the life of me figure out why this doesn't work:

    Code JavaScript:
    var ids = new Array({$js_string});
     
    $('#check_all').click(function()	 {
     
    	$.each(ids, function()	{
     
    		if ($('#' + this).is(':checked'))	{
    			$('#' + this).attr('checked', true);
    			}
    		else	{
    			$('#' + this).attr('checked', false);
    			}
     
    		});
    	return false;
    	});

    BTW, the array that is created is done in PHP. I have done alerts inside the if and the else and they fire fine and the ids are there, it's the code to check them that isn't working and I just don't get it!

    Any pointers would sure be appreciated

  2. #2
    SitePoint Addict
    Join Date
    Dec 2005
    Posts
    336
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Maybe:
    Code JavaScript:
    if ($('#' + this).is(':checked')) {
    	$('#' + this).attr('checked', 'checked');
    } else {
    	$('#' + this).removeAttr('checked');
    }

  3. #3
    SitePoint Wizard gold trophysilver trophybronze trophy dc dalton's Avatar
    Join Date
    Nov 2004
    Location
    Right behind you, watching, always watching.
    Posts
    5,431
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by centered effect View Post
    Maybe:
    Code JavaScript:
    if ($('#' + this).is(':checked')) {
    	$('#' + this).attr('checked', 'checked');
    } else {
    	$('#' + this).removeAttr('checked');
    }
    Yeah, sorry, I should have mentioned I tried that ... no love there either

  4. #4
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,526
    Mentioned
    83 Post(s)
    Tagged
    3 Thread(s)
    If the checkbox is already checked, then you make it checked.
    Else, if the checkbox is not checked, then you make it not checked.

    That's what your code is doing, which doesn't change things at all.

    Have a think about what you actually want to occur instead.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  5. #5
    SitePoint Wizard gold trophysilver trophybronze trophy dc dalton's Avatar
    Join Date
    Nov 2004
    Location
    Right behind you, watching, always watching.
    Posts
    5,431
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by paul_wilkins View Post
    If the checkbox is already checked, then you make it checked.
    Else, if the checkbox is not checked, then you make it not checked.

    That's what your code is doing, which doesn't change things at all.

    Have a think about what you actually want to occur instead.
    Oy!

    There is is, as plain as the nose on my face!

    That's what happens when I stare at code too long (or as my wife would say: "you want to hide something from him? Put it right under his nose")

    Thanks for the extra eyes ... that's what I needed!

  6. #6
    SitePoint Addict
    Join Date
    Dec 2005
    Posts
    336
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Thanks Paul, maybe I needed a second set of eyes too!

  7. #7
    SitePoint Wizard gold trophysilver trophybronze trophy dc dalton's Avatar
    Join Date
    Nov 2004
    Location
    Right behind you, watching, always watching.
    Posts
    5,431
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by centered effect View Post
    Thanks Paul, maybe I needed a second set of eyes too!
    Don't think it was just you and I ... 56 views on this thread and only Paul caught it


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •