Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check/Uncheck checkbox with JavaScript

How can a checkbox be checked/unchecked using JavaScript?

like image 589
lisovaccaro Avatar asked Nov 21 '11 02:11

lisovaccaro


People also ask

How do you check and uncheck a checkbox?

Once the checkbox is selected, we are calling prop() function as prop( "checked", true ) to check the checkbox and prop( "checked", false ) to uncheck the checkbox.

How do you check and uncheck a checkbox in HTML?

attr("checked","checked"); To uncheck the checkbox: $("#checkboxid"). removeAttr("checked");

How do you uncheck a box in JavaScript?

<input type="checkbox" name="deadline" value="yes" <? = $checked ?> >


1 Answers

Javascript:

// Check document.getElementById("checkbox").checked = true;  // Uncheck document.getElementById("checkbox").checked = false; 

jQuery (1.6+):

// Check $("#checkbox").prop("checked", true);  // Uncheck $("#checkbox").prop("checked", false); 

jQuery (1.5-):

// Check $("#checkbox").attr("checked", true);  // Uncheck $("#checkbox").attr("checked", false); 
like image 147
Alex Peattie Avatar answered Sep 28 '22 05:09

Alex Peattie