MID
MID

Reputation: 1825

How to set focus on first input field using jQuery in IE?

I can set focus on first input, that works in all browsers(except IE):

  $('#login').on('shown', function () {
  $(login).find('input:visible:first').focus();
})

I need to call it after Bootstrap modal showing will be finished, so I'm calling it in shown function.

Also tried this code(not working):

 $('#sign_up').on('shown', function () {
    setTimeout(function () {
    $(sign_up).find('input:visible:first').focus();
  }, 100);
 ///working everywhere except explorer
$('#login').on('shown', function () {
  $('#user_email').focus();
})

Upvotes: 0

Views: 4689

Answers (1)

Afshin
Afshin

Reputation: 4215

javascript

<script type="text/javascript">
function formfocus() {
  document.getElementById('element').focus();
}
window.onload = formfocus;
</script>

JAVASCRIPT DEMO

jquery

$(document).ready(function(){
  $('#element').focus();
});

JQUERY DEMO

JQUERY FOR IE 8

$(document).ready(function(){
  setTimeout(function() {
     $('#element').focus();
  }, 10);
});

IE 8 DEMO

HTML

<form>
  <input id="element" />
  <input />
  <input />
</form>

Upvotes: 3

Related Questions