4607

I'd like to do something like this to tick a checkbox using jQuery:

$(".myCheckBox").checked(true); 

or

$(".myCheckBox").selected(true); 

Does such a thing exist?

7
  • A more specific (and very useful!) question, "How do I check a item in a checkbox-set BY VALUE?", I think we can also discuss here, and I posted an answer below. Commented Mar 9, 2012 at 12:36
  • Check other ways to do this using jQuery here stackoverflow.com/a/22019103/1868660 Commented Feb 25, 2014 at 15:46
  • If you need the onchange event triggered, it's $("#mycheckbox").click(); Commented May 11, 2015 at 14:06
  • "Checking something" suggests testing it, so I think 'Making a checkbox checked' is a more clear and better title. Commented May 12, 2015 at 17:26
  • prop(); function is the perfect answer. See the function definition - api.jquery.com/prop Commented May 23, 2016 at 15:01

44 Answers 44

1
2
11

In jQuery,

if($("#checkboxId").is(':checked')){ alert("Checked"); } 

or

if($("#checkboxId").attr('checked')==true){ alert("Checked"); } 

In JavaScript,

if (document.getElementById("checkboxID").checked){ alert("Checked"); } 
Sign up to request clarification or add additional context in comments.

2 Comments

This does not answer the question.
This answer is out-of-date because it uses .attr instead of .prop.
11

This may help someone.

HTML5

 <input id="check_box" type="checkbox" onclick="handleOnClick()"> 

JavaScript.

 function handleOnClick(){ if($("#check_box").prop('checked')) { console.log("current state: checked"); } else { console.log("current state: unchecked"); } } 

Comments

11

if($('jquery_selector').is(":checked")){ //somecode }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

1 Comment

When giving an answer it is preferable to give some explanation as to WHY your answer is the one.
11

If you are using .prop('checked', true|false) and don’t have changed checkbox, you need to add trigger('click') like this:

// Check $('#checkboxF1').prop( "checked", true).trigger('click'); // Uncheck $('#checkboxF1').prop( "checked", false).trigger('click'); 

1 Comment

so happy that I found this. thank you! almost went nuts!!
9
$(".myCheckBox").prop("checked","checked"); 

1 Comment

This answer is out-of-date because it uses .attr instead of .prop.
7

You can check a checkbox checked condition using JavaScript in different ways. You can see below.

  1. First method - $('.myCheckbox').prop('checked', true);

  2. Second method - $('.myCheckbox').attr('checked', true);

  3. Third method (for check condition if checkbox is checked or not) - $('.myCheckbox').is(':checked')

Comments

6

You can try this:

$('input[name="activity[task_state]"]').val("specify the value you want to check ") 

Comments

6

Edited on 2019 January

You can use: .prop( propertyName ) - version added: 1.6

p {margin: 20px 0 0;} b {color: red;} label {color: red;}
<!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <script src="https://code.jquery.com/jquery-1.10.2.js"></script> </head> <body> <input id="check1" type="checkbox" checked="checked"> <label for="check1">Check here</label> <p></p> <script> $( "input" ).change(function() { var $input = $( this ); $( "p" ).html( "The .attr( \"checked\" ): <b>" + $input.attr( "checked" ) + "</b><br>" + "The .prop( \"checked\" ): <b>" + $input.prop( "checked" ) + "</b><br>" + "The .is( \":checked\" ): <b>" + $input.is( ":checked" ) + "</b>" ); }).change(); </script> </body> </html>

On Angular Framework

Example 1

In your .html file

<input type="checkbox" (change)="toggleEditable($event)"> 

In your .ts file

toggleEditable(event) { if ( event.target.checked ) { this.contentEditable = true; } } 

Example 2

In your .html file

<input type="checkbox" [(ngModel)]="isChecked" (change)="checkAction(isChecked ? 'Action1':'Action2')" /> 

Comments

6

If you happen to be using Bootstrap (perhaps unawarely) ...

$('#myCheckbox').bootstrapToggle('on') $('#myCheckbox').bootstrapToggle('off') 

http://www.bootstraptoggle.com/

Comments

5

I was wondering why none of the old answers mentioned that to fully simulate the ticking, you also need to simulate the change event if you want to fire something else after simulating ticking:

$('.myCheckbox').prop('checked', true).trigger("change"); $('.myCheckbox').prop('checked', false).trigger("change"); 

Comments

2

You can do this if you have the id to check it

document.getElementById('ElementId').checked = false

And this to uncheck

document.getElementById('ElementId').checked = true

1 Comment

It works, but that's not about jquery.
1

A JavaScript solution can be also simple and with less overhead:

document.querySelectorAll('.myCheckBox').forEach(x=> x.checked=1) 

document.querySelectorAll('.myCheckBox').forEach(x=> x.checked=1)
checked A: <input type="checkbox" class="myCheckBox"><br/> unchecked: <input type="checkbox"><br/> checked B: <input type="checkbox" class="myCheckBox"><br/>

1 Comment

1

If you consider using vanilla js instead of jquery there is a solution:

//for one element: document.querySelector('.myCheckBox').checked = true /* or false */ //will select the first matched element //for multiple elements: for (const checkbox of document.querySelectorAll('.myCheckBox')) { checkbox.checked = true //or false } 

Comments

-6

For overall:

$("#checkAll").click(function(){ $(".somecheckBoxes").prop('checked',$(this).prop('checked')?true:false); }); 

Comments

1
2

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.