JQuery UI modal dialogue, focus on the first form element

I am currently using jQuery UI modal box.

I am wondering how I can set focus on the first form element of a modal window when opening a dialog, I think this should happen by default, but for some reason it is not.

How to configure jquery ui on the first form element when opening?

here is the URL of the page with the modal dialog, just click the "Show dialog" link on this page

+7
source share
7 answers

You can use an open event in the jquery ui dialog and set focus to the input id. You can do something like this.

$( ".selector" ).dialog({ open: function(event, ui) { $('#target').focus(); } }); 
+15
source

Add a snap function to the event shown, then set the focus

 $('#login_modal').on('shown', function () { $("#login_modal input").first().focus(); }); $('#login_modal').modal(); 
+8
source

Thanks for the answer, in the end I had to focus using the event callback 'shown .bs.modal' to add focus to the element.

  $('#login-modal').on('shown.bs.modal', function () { $("#user_session_email").focus(); }); 
+3
source

By default, jQuery UI Modal will focus on the first input field in modal mode.

If for some reason the first input field is not set, you can add the input autofocus attribute in the first input field:

 <input type="text" name="date" value="" autofocus> <input type="text" name="phone" value=""> 

Or if you need a second or other input field instead of focus, apply the autofocus input attribute to the second input field:

 <input type="text" name="date" value=""> <input type="text" name="phone" value="" autofocus> 

:)

+2
source

try this, focus on jQuery Modal:

 setTimeout(function () { $('#cntrlId').focus(); }, 1); 
+2
source
0
source

Add function in the dialog box that opens

 $("#dialogMensaje").dialog({width: 600, title: "NotificaciΓ³n", modal: true, buttons: { "Enviar": function() { $(this).dialog("close"); } }, open: function() { setTimeout(function() { $('#txt').focus(); }, 420); } }); 
0
source

All Articles