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?
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"); } .attr instead of .prop.if($('jquery_selector').is(":checked")){ //somecode } <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 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'); $(".myCheckBox").prop("checked","checked"); .attr instead of .prop.You can check a checkbox checked condition using JavaScript in different ways. You can see below.
First method - $('.myCheckbox').prop('checked', true);
Second method - $('.myCheckbox').attr('checked', true);
Third method (for check condition if checkbox is checked or not) - $('.myCheckbox').is(':checked')
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> In your .html file
<input type="checkbox" (change)="toggleEditable($event)"> In your .ts file
toggleEditable(event) { if ( event.target.checked ) { this.contentEditable = true; } } In your .html file
<input type="checkbox" [(ngModel)]="isChecked" (change)="checkAction(isChecked ? 'Action1':'Action2')" /> If you happen to be using Bootstrap (perhaps unawarely) ...
$('#myCheckbox').bootstrapToggle('on') $('#myCheckbox').bootstrapToggle('off') 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"); 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
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/> 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 }
$("#mycheckbox").click();