Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jquery UI modal dialog, set focus on first form element

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

like image 803
sat Avatar asked Jun 09 '11 10:06

sat


People also ask

How to focus on modal jQuery?

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.

How do you focus on modal elements?

tab, shift + tab and enter key and focus should be trapped inside modal and should not go out after pressing tab key multiple times.

How to set focus on input field using jQuery?

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.

What is dialog in jQuery?

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.


4 Answers

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>

:)

like image 159
Jonathan Marzullo Avatar answered Oct 25 '22 01:10

Jonathan Marzullo


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(); }
});
like image 43
Abdul Kader Avatar answered Oct 25 '22 01:10

Abdul Kader


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();
like image 41
silver Avatar answered Oct 24 '22 23:10

silver


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();
    });
like image 34
Paul Barclay Avatar answered Oct 25 '22 01:10

Paul Barclay