I have a jquery function so whenever a <input type="text">
element comes into focus, all text within it is selected.
$('input[type=text]').focusin(function(e) {
// Selects all text on text field focus
$(this).select();
});
So far, so good. Elsewhere in the page is a checkbox disguised as toggle switches via some fancy CSS, using the topcoat css library. Here is what the toggle switch looks like:
When I click or tab into a textbox, all the text is selected, as expected. However, if I then click onto the toggle switch, it won't fire. I have to double click to make it work. It should only require a single click. If I click out of the textbox, onto the background of the page, the toggle switch reverts to only needing a single click. If I reload the page, and don't click into any text boxes, the toggle switch works properly, only requiring a single click. Weirdly enough, if I remove the $(this).select();
line from the jQuery function, the toggle switch then functions correctly again.
This is the structure of the HTML:
<div class="section-wrapper">
<div class="section">
<div class="trough">
<label class="topcoat-switch">
<input type="checkbox" class="topcoat-switch__input">
<div class="topcoat-switch__toggle"></div>
</label>
</div>
</div>
<div class="section-contents">
<!-- Input text fields are in here, buried in some more divs -->
</div>
</div>
To dig deeper, I wrote a small function to keep track of which elements were registering a click.
$('div, input').on('click', function(e) {
console.dir(e.currentTarget);
})
When working as expected, before focusing in on any text boxes, this is what the mouse click propagation looks like when clicking the toggle switch:
input.topcoat-switch__input
div.trough
div.section
div.section-wrapper
div.container-inner
div.container
The crucial element that needs to register a click is the input element:
input.topcoat-switch__input
As expected, the mouse click registers in the deepest element, and propagates outward. However, after I click into a text box and then click the toggle switch, the mouse propagation stack looks like this:
div.topcoat-switch__toggle
div.trough
div.section
div.section-wrapper
div.container-inner
div.container
The input element is nowhere to be seen, and requires a double click to register. Instead of the input registering, it seems as though the div.topcoat-switch__toggle
element (the input's sibling element) is registering instead.
What can I do to make it so I can keep the functionality of selecting all text when focusing on a text input, and fix the weird mouse click registration behavior?