I have styled input:focus + label to visually indicate when an input has been focused using keyboard navigation.
But Chrome (and maybe not just Chrome?) persists the :focus state after using a mouse to click the input, which looks ugly.
I want to remove the :focus styling after the click without altering keyboard navigation. i.e., when you move your mouse away after clicking a label, it should no longer be red.
I have seen suggestions to .blur() the input on mouseup; but 1) it hasn't worked for me (see snippet below), and 2) I'm concerned it will take keyboard navigation out of its usual flow.
Yes, someone using the mouse to click a checkbox and then going to keyboard navigation is an edge case. But it's one I would like to account for.
$("label").mouseup(function () {
$(this).blur(); // This has no apparent affect.
$("#" + this.htmlFor).blur(); // Or this.
});
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>