1

I'm trying to set focus on an input after the modal shows.

The solutions and links I've seen and tried so far: How to focus on a form input text field on page load using jQuery?, Jquery Focus on input field, and none of them works.

My code is actually pretty simple:

$input = $("#letter-change-text").find("input[type='text']");
$input.val($.trim("I am just testing!!"));
$("#letter-change-text").modal('show');
$input.focus(); // this should focus the textbox in order to allow immediately input by the user

You can check this issue in JSfiddle.

I also have tried without any success:

setTimeout(function(){
   $input.focus();
}, 0);
Community
  • 1
  • 1
Linesofcode
  • 5,327
  • 13
  • 62
  • 116

2 Answers2

2

Use the shown event

$(function(){
    $input = $("#letter-change-text").find("input[type='text']");
    $input.val($.trim("I am just testing!!"));
    $('#letter-change-text').on('shown.bs.modal', function (e) {
       $input.focus();
    }).modal('show');
});
epascarello
  • 204,599
  • 20
  • 195
  • 236
0

You can just try

$("#dgRejectSalvage").on('shown.bs.modal', function () {
    $(this).find('#RejectionReason').focus();
});
Josh Mein
  • 28,107
  • 15
  • 76
  • 87