How to adjust focus on the first <select> element on a page using jQuery?

I would like to focus on the first element of the page using jQuery when the page is loaded. I could successfully set the focus for the first element using

$(":input:visible:first").focus(); 

or

$(':input:enabled:visible:first').focus();

according to jquery, focus on the first allowed input or select or text box on the page .

I tried using any of the below:

$('select:first').focus();
$("select").first().focus();
$(':visible:first').focus();
$('enabled:visible:first').focus();

or other options that I could think of, but failed.

Can someone help me in solving this problem? I am new to jQuery.

I would like this feature to work for all major browsers.

Many thanks!

+5
source share
2 answers

, DOM. :

$(document).ready(function(){
   $("select:first").focus();
});

.

EDIT:. , :

$(function(){
  $("select:first").focus();
})

, jQuery , DOM .

+8

, DOM. ready.

jQuery(document).ready(function() {
    $(":input:visible:first").focus();               
});

a >

UPDATE:

@David , .

$(document).ready(function(){
    $("select:first").focus();
});

jquery ( ). http script src.

<script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script>
+2

All Articles