I stylized input:focus + label to visually indicate when the input was focused using keyboard navigation.
But Chrome (and maybe not just Chrome?) Remains able to :focus after using the mouse to click input, which looks ugly.
I want to remove the :focus style after clicking without changing the keyboard navigation. that is, when you click the mouse button after clicking the tag, it should no longer be red.
I saw suggestions .blur() input on mouseup; but 1) it didn’t work for me (see snippet below), and 2) I am worried that the keyboard will exit the normal stream.
Yes, someone using a mouse to click a checkbox and then go to the keyboard is the edge. But I would like to explain this.
$("label").mouseup(function () { $(this).blur();
input:hover + label, input:focus + label { color: red; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <form> <p>Form Part One</p> <input type="checkbox" id="cb1" name="cb" /><label for="cb1">First Checkbox</label><br> <input type="checkbox" id="cb2" name="cb" /><label for="cb2">Second Checkbox</label> <p>Form Part Two</p> <input type="radio" id="r1" name="r" /><label for="r1">First Radio</label><br> <input type="radio" id="r2" name="r" /><label for="r2">Second Radio</label> </form>
source share