I have a site with one textfield for search however I dont want the focus to be on it when the page loads however it is the only text input on the form, is it possible to remove the focus?
The blur() method removes focus from an element.
To disable focus on a specific element, set the tabIndex property on the element to a value of -1 . The tabIndex property can be used to specify that an element cannot be focused using keyboard navigation. Here is the HTML for the examples in this article. Copied!
To remove or disable focus border of browser with CSS, we select the styles for the :focus pseudo-class. to set the outline style to none to remove the border of the element that's in focus.
to set the onSubmitEditing prop to Keyboard. dismiss . As a result, when we press enter when we're focus on the text input, the focus will move away from the text input.
A jQuery solution would be something like:
$(function () {
$('input').blur();
});
use document.activeElement.blur();
example at http://jsfiddle.net/vGGdV/5/ that shows the currently focused element as well.
Keep a note though that calling blur()
on the body element in IE will make the IE lose focus
I would add that HTMLElement has a built-in .blur
method as well.
Here's a demo using both .focus
and .blur
which work in similar ways.
const input = document.querySelector("#myInput");
<input id="myInput" value="Some Input">
<button type="button" onclick="input.focus()">Focus</button>
<button type="button" onclick="input.blur()">Lose focus</button>
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