Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I stop a HTML checkbox from getting focus on click?

Anyone know how to prevent an HTML checkbox from getting focus when clicked? I still want it to change state, just not get focus.

like image 225
David Tinker Avatar asked Nov 08 '12 06:11

David Tinker


People also ask

How can I prevent checkbox from being checked on click?

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();

How do I adjust a checkbox in HTML?

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.

How do I unselect a checkbox in HTML?

$("#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.

How do I make a checkbox always checked in HTML?

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.


1 Answers

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
}
like image 51
Konstantin Dinev Avatar answered Sep 19 '22 18:09

Konstantin Dinev