Exitos
Exitos

Reputation: 29750

Is it possible to remove the focus from a text input when a page loads?

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?

Upvotes: 84

Views: 196954

Answers (5)

Ivan
Ivan

Reputation: 40768

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>

Upvotes: 15

Coin_op
Coin_op

Reputation: 10728

A jQuery solution would be something like:

$(function () {
    $('input').blur();
});

Upvotes: 122

Gabriele Petrioli
Gabriele Petrioli

Reputation: 196276

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

Upvotes: 97

Shadow Wizzard
Shadow Wizzard

Reputation: 66397

$(function() {
 $("#MyTextBox").blur();
});

Upvotes: 9

jammus
jammus

Reputation: 2550

You can use the .blur() method. See http://api.jquery.com/blur/

Upvotes: 12

Related Questions