Anyone know how to prevent an HTML checkbox from getting focus when clicked? I still want it to change state, just not get focus.
In other words, the checkbox should be visible as a modifiable control but the user clicks should not change the state of the checkbox. In such cases, disabling the checkbox is not an option. Clicks on either of the checkboxes will be ignored because of e. preventDefault();
Method 1: The checkbox size can be set by using height and width property. The height property sets the height of checkbox and width property sets the width of the checkbox.
$("#checkboxid"). attr("checked","checked"); To uncheck the checkbox: The other answers hint at the solution and point you to documentation that after further digging will get you to this answer.
The checked attribute is a boolean attribute. When present, it specifies that an <input> element should be pre-selected (checked) when the page loads. The checked attribute can be used with <input type="checkbox"> and <input type="radio"> . The checked attribute can also be set after the page load, with a JavaScript.
You can prevent the focus by using preventDefault() in a mousedown handler:
$('input[type=checkbox]').mousedown(function (event) {
// Toggle checkstate logic
event.preventDefault(); // this would stop mousedown from continuing and would not focus
});
In pure JavaScript:
checkbox.onmousedown = function (event) {
// Toggle checkstate logic
event.preventDefault(); // this would stop mousedown from continuing and would not focus
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With