Olivier J.
Olivier J.

Reputation: 3165

Check checkboxes with JQuery in JSF context

I just would like to check all my checkboxes with JQuery. These checkboxes are created with JSF.

I tried :

$("input[type=checkbox]").each( 
        function() {              
           $(this).prop('checked', true);
// OR
           $(this).attr('checked', true);
// OR         
           $(this).attr('checked', 'checked');
          alert($(this).attr('id'));
            } 
        );

nothing works...

Can JSF be the problem ? Value of these checkboxes are linked with some managed bean boolean value.

Thanks for help.

PS : my alert() function returns id of checkboxes properly and JQuery version is :

jQuery JavaScript Library v1.8.1

Upvotes: 0

Views: 3086

Answers (3)

Olivier J.
Olivier J.

Reputation: 3165

Ok, I think I have understood the problem...

when I try this code :

$(":checkbox").prop('checked',true);
$("input[type=checkbox]").each( 
        function() {          
          alert($(this).prop('checked'));               
        } 
    );

I have always true values but I see some checkboxes unchecked...

So values are changed but visual stay the same. It's because I use JSF and it's folowwed render :

enter image description here enter image description here

This checkox is made of some CSS background etc...

So I have to change CSS of parent div (with background...) to make this checkox checked visually since its value is already set to true.

It was just a visual problem, not value problem.

Thank you

Upvotes: 1

Elias Dorneles
Elias Dorneles

Reputation: 23806

Try using the plain DOM:

$("input[type=checkbox]").each(function() { this.checked = true; });

See also:

Setting "checked" for a checkbox with jQuery?

Upvotes: 0

Claudio Redi
Claudio Redi

Reputation: 68400

This should do the trick

$(":checkbox").prop('checked',true);

DEMO

Upvotes: 2

Related Questions