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?
5 Answers
A jQuery solution would be something like:
$(function () {
$('input').blur();
});
-
actually this code has a typo $(function(){ $('input).blur(); }); Feb 11, 2013 at 18:17
-
-
-
The question had a "jQuery" tag, but blur() and focus() are JavaScript DOM Methods so they should be available with most frameworks (like they are with jQuery) as well as with straight JavaScript. (CSS has a ":focus" pseudo-class selector, but that's more like an adjective rather than a verb.)– kjpiresApr 7, 2021 at 21:11
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
-
2It work as exprected in Safari MAC OS X 10.12, iPhone iOS 9,10,10.2! @Gaby aka G. Petrioli Feb 7, 2017 at 7:59
-
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>