1

I have two input checkboxes, and I want to check/uncheck them with jQuery:

<input type="checkbox" name="user1" value="1" id="u1" onclick="loadUserCalendar(1)">
<input type="checkbox" name="user2" value="2" id="u2" onclick="loadUserCalendar(2)">

JavaScript code:

var list = new cookieList("calendar_users"); 
var users = list.items();
for (var i = 0; i < users.length; i++) {
    $('input[name="user' + users[i].substr(1,1) + '"]').attr('checked', true);
    console.log('substring: ' + users[i].substr(1,1));
    console.log('enabling user ' + users[i]);
}

var users contains these values: u1,u2 . To only get the user ID, I perform a substring, which gets me the correct number as you can see below in the console output.

Console output:

substring: 1
enabling user u1
substring: 2 
enabling user u2 

I have no idea, why the checkboxes are not checked when the code ran. What am I doing wrong here?

Edit: I am using jQuery 1.4.4 due to compatibility reasons, that's why I am using .attr()

5 Answers 5

2

Inline event handlers (onclick="loadUserCalendar(1)") are not the jQuery way. You should not have any in your html. Get rid of them and add to your jquery:

$(function() {
    $('form').on('click',':checkbox', function() {
        loadUserCalendar($(this).val());
    });
});

But maybe what you wanted was this:

$(function() {
    $('form').on('click',':checkbox', function() {
        $('form :checkbox').attr('checked','checked');
    });
});

The jquery 1.ancient version:

$(function() {
    $('input[type=checkbox]').live('click', function() {
        // do things
    });
});
Sign up to request clarification or add additional context in comments.

7 Comments

It says this: Uncaught TypeError: Object #<Object> has no method 'on'
What version of jQuery are you using? 1.4.potato?
I am using jQuery 1.4.4 due to compatibility reasons
You mean due to incompatibility reasons, lol. I've updated my answer. Please consider updating your compatibility.
Thanks for this. This fixed the "not jQuery way". If I reload my page, I lose the checked checkboxes again, and that was my main problem. Do you also have an answer for this?
|
0

What version of jQuery are you using?

You have to try something like :

$('input[name="user' + users[i].substr(1,1) + '"]').attr('checked', 'checked');

or

$('input[name="user' + users[i].substr(1,1) + '"]').prop('checked', 'checked');

The second one is jQuery 1.6+

1 Comment

Hey, I tried the first approach but still no result. The checkbox is not being checked. The second one cannot be used as I am using jQuery 1.4.4.
0

When you want something to be checked you have to do the following:

 $('.myCheckbox').attr('checked','checked');

3 Comments

Please make shure that you have the right selector $('!your selector!').attr('checked', 'checked);
This is exactly the question and that's why I posted this question. If you think the selector is wrong, please tell me the correct one and I will accept it as answer.
Maybe its a JQuery problem. I would suggest you not using such complex statements. Rather use the id selector with #u1, #u2, ...
0

If you have created the checkboxes dynamically use jQuery on method, because simple functions won't be able to access these elements.

1 Comment

live has been deprecated for centuries.
0

Regarding the initialization of the checkboxes I would suggest something like this:

$('input[type=checkbox]').each(function(index, box) {
    $(box).attr('checked', $.inArray($(box).attr("id"), users) != -1);
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.