user3035279
user3035279

Reputation: 189

How to change checkbox attribut 'checked'?

I have some web page with input fields in it and one of them is a checkbox, I'm trying to create 'clear all' button to clear all values including checkbox 'v'. I tried $('#check5').removeAttr('checked'); and $('#check5').attr('checked',false);.

But it works only after pressing F5, and I would like to change the attribute status without refreshing the page. Any idea ?

Upvotes: 1

Views: 161

Answers (6)

Sourav Kumar Hazra
Sourav Kumar Hazra

Reputation: 11

You can use this code:

To uncheck:

$('#check5').prop('checked',false);

To check:

$('#check5').prop('checked',true);

Upvotes: 1

Abdur Rehman
Abdur Rehman

Reputation: 3293

following code is working for me.

$(':input').not(':button, :submit').val('').removeAttr('checked').removeAttr('selected');

It clear all the input type fields inside form.

Upvotes: 0

CCoder
CCoder

Reputation: 171

If you are aiming at the specific checkbox,

$('#check5').prop('checked',false);

If not, below given resets all the checkboxes in the page.

$("input[type=checkbox]").prop("checked", false);

Upvotes: 1

Sean
Sean

Reputation: 1464

The checked attribute is a property, try using this:

$("input[type=checkbox]").prop("checked", false);

this will make all checkbox's on a page unchecked.

Upvotes: 1

Chandra Kumar
Chandra Kumar

Reputation: 4205

If you mean how to remove the 'checked' state from all checkboxes:

$('input:checkbox').removeAttr('checked');

Example:

$(document).ready(function(){
    $('.check:button').toggle(function(){
        $('input:checkbox').attr('checked','checked');
        $(this).val('uncheck all');
    },function(){
        $('input:checkbox').removeAttr('checked');
        $(this).val('check all');        
    })
})

DEMO

Upvotes: 2

Jay
Jay

Reputation: 723

You can use this

$('#check5').prop('checked', false);

Upvotes: 4

Related Questions