I am currently using the jquery UI modal box.
I am wondering how I can set the focus onto the first form element of the modal box when opening the dialog I think this is meant to happen by default but for some reason it is not.
How can I set the jquery ui to focus on the first form element when opening?
here is the url to the page with the modal dialog just click the Show the Dialog link on this page
Answer: Use the jQuery . focus() method You can simply use the jQuery . focus() method to set the focus on the first input box or textarea inside the Bootstrap modal when it is loaded upon activation.
tab, shift + tab and enter key and focus should be trapped inside modal and should not go out after pressing tab key multiple times.
Example 1: In this example the form input text field gets the focus as page loads by using focus() method . Here the input element is selected by input keyword in JQuery selector. | Set focus on a form input text field on page load. This input box gets the focus as the page loads.
A dialog box is a floating window with a title and content area. This window can be moved, resized, and of course, closed using "X" icon by default. jQueryUI provides dialog() method that transforms the HTML code written on the page into HTML code to display a dialog box.
By default the jQuery UI Modal will give focus to the first input field in the modal.
If for some reason the first input field is not given focus, you can add the input attribute autofocus on the first input field:
<input type="text" name="date" value="" autofocus>
<input type="text" name="phone" value="">
Or if you need the second or another input field to have focus instead, then apply the input attribute autofocus to the second input field:
<input type="text" name="date" value="">
<input type="text" name="phone" value="" autofocus>
:)
You can make use of open event in jquery ui dialog and set the focus to the input id . You can do something like this.
$( ".selector" ).dialog({
open: function(event, ui) { $('#target').focus(); }
});
Add a function bind to the shown event, then set the focus
$('#login_modal').on('shown', function () {
$("#login_modal input").first().focus();
});
$('#login_modal').modal();
Thanks for the reply, in the end I had to focus using the event callback 'shown.bs.modal' to add the focus to the element.
$('#login-modal').on('shown.bs.modal', function () {
$("#user_session_email").focus();
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With