sichinumi
sichinumi

Reputation: 1875

Disable Text Field Autofocus in Javascript

Is there a way in Javascript to forcibly disable text field autofocus on page load?

I know of the focus() method, but not how to disable it. My actual problem is that autofocusing in a modal window forces the window to appear with the scrollbar halfway down already and scrolled to the first input field, whereas I would like to disable this entirely.

Thanks!

Upvotes: 7

Views: 29551

Answers (4)

Derek 朕會功夫
Derek 朕會功夫

Reputation: 94409

You can use .blur() ,

var elelist = document.getElementsByTagName("input");
for(var i = 0; i < elelist.length; i++){
    elelist[i].blur();
}

If you want to disable focus on all the textfields,

var elelist = document.getElementsByTagName("input");
for(var i = 0; i < elelist.length; i++){
    elelist[i].addEventListener("focus", function(){
        this.blur();
    });
}

Upvotes: 7

Shawn Steward
Shawn Steward

Reputation: 6825

There's something in the Javascript code you're using that's setting the focus to the field, this is not an automatic behavior. Just find that and disable it.

Upvotes: 2

Warface
Warface

Reputation: 5119

You can use in jQuery

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

Upvotes: 5

Jonathan M
Jonathan M

Reputation: 17461

You could force focus on an object higher in the page on load.

Upvotes: 1

Related Questions