views:

151

answers:

3

Hello all, I am having trouble checking checkboxes via JQuery - I have done this:

if(element[1]==3)
  alert('#'+element[0]+'_1');//shows #chk_1
  $('#'+element[0]+'_1').attr('checked', true);
  $('#'+element[0]+'_2').attr('checked', true);
  $('#'+element[0]+'_3').attr('checked', true);    
}

I know it gets to that point because I can see the alert window with the correct variable name. I have also included JQuery and my firebug isn't complaining about any errors, its just nothing is happening.

I have a few other functions which are active on a user click:

onclick="checker($(this).attr('id'));" 

Which essentially checks if a user is allowed to tick the checkbox but this shouldn't get activated if I use the above method, correct? Plus I have done the automated ticks in such a way that those rules are preserved and the user is allowed to tick the checkboxes.

I am able to click the checkboxes and those rule checks are done correctly in the checker function.

HELP! I have no idea what's wrong to be frank.

I appreciate nay help.

Update

With Pekka's advice I have found that no JQuery function seems to be working for example:

$('#'+element[0]+'_1').hide();

Did nothing. So it means the ID of the element has a problem? But the element does exist and its the correct name!

I have given the checkbox a value and tried this:

alert($('#'+element[0]+'_1').val());

I get "undefined" returned! Why?

Update 2

Testing if it has been checked:

$('#'+element[0]+'_1').attr('checked', true);

if ($('#'+element[0]+'_1'+':checked').val() !== null) {
   alert('#'+element[0]+'_1');
}

This showed me the alert box. So it seems it is checked but I can not see it?

Update - Solution

Don't be a noob like me and wrap JQuery code with: $(document).ready(function() {

I managed to waste everyones time having not done this, I think that bit of code is going to become standard for me from now on.

Thank you everyone for the continued help! :) I'll have to give to the person [lillq] that mentioned document load!

A: 

Try ('#'+element[0]+'_1').click();

Jonathan Julian
What if it's checked already? Won't this uncheck it in that case?
Pekka
You could use :not(:checked) to only select unchecked inputs
Simon
A: 

Changing the checked state could perhaps trigger an onchange event, but certainly not a click event, so that is irrelevant.

You should not set the attribute to true but "checked".

$('#'+element[0]+'_1').attr('checked', 'checked');

See this FAQ entry.

Guffa
I tried that already, but that doesn't work either! But I should be using that as best practice?
Abs
@Abs: According to the jQuery FAQ, yes you should. If it still doesn't work, verify that the element actually has that id (not just as name) and that you don't have duplicate id's.
Guffa
**The FAQ is wrong.** `attr` sets (for the most part, except for a couple of workarounds) JavaScript properties and **not** HTML attributes. This is clear: if you set the `checked` HTML attribute that *does not affect* the checkedness of the checkbox, only the `defaultChecked`​ness to which that attribute maps. jQuery is setting the property, and thus the correct value to set it to is `true`. Though `'checked'`, like any other non-empty string value will still work because it is truthy.
bobince
I can confirm that I do not have duplicate elements, that a checkbox with that ID exists and is not duplicated.
Abs
+2  A: 

One thought is:

If you have more than one chk_1, chk_2, chk_3 ids in your html, the $('#'+element[0]+'_2').attr('checked', true); will only effect the first one in your document.

I ran the code you supplied as such:

html

...
<body>
    <input type="checkbox" id="el1" onclick="checker($(this).attr('id'));" />Text<br />
    <input type="checkbox" id="el2" onclick="checker($(this).attr('id'));" />Text<br />
    <input type="checkbox" id="el3" onclick="checker($(this).attr('id'));" />Text<br />
</body>
</html>
<script type="text/javascript">
    init();
</script>

javascript

function init () {
    $('#el1').attr('checked', true); 
    $('#el2').attr('checked', true); 
    $('#el3').attr('checked', true); 
}
function checker(id)
{
    alert(id);
}

The init function here checks all of the boxes after the document has loaded. Is there any other info that you could give us help out.

I like to reduce examples like this to the simplest code that fails.

lillq
OMFG thank you for mentioning document has loaded - I wrapped my JS functions with `$(document).ready(function() {` and everything works now! I can't believe this. What a waste of time, apologies everyone as well! I will edit my question.
Abs
Glad to help out.
lillq