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!