views:

3095

answers:

5

I have setup a jQuery UI modal dialog to display when a user clicks a link. There are two textboxes (I only show the code for 1 for brevity) in that dialog div tag and it is changed to be a jQuery UI DatePicker textbox that reacts on focus.

The problem is that the jQuery UI dialog('open') somehow triggers the first textbox to have focus, which then triggers the datepicker calendar to open immediately.

So I am looking for a way to prevent the focus from happening automatically.

    <div><a id="lnkAddReservation" href="#">Add reservation</a></div>

    <div id="divNewReservation" style="display:none" title="Add reservation">
        <table>
            <tr>
                <th><asp:Label AssociatedControlID="txtStartDate" runat="server" Text="Start date" /></th>
                <td>
                    <asp:TextBox ID="txtStartDate" runat="server" CssClass="datepicker" />
                </td>
            </tr>
        </table>

        <div>
            <asp:Button ID="btnAddReservation" runat="server" OnClick="btnAddReservation_Click" Text="Add reservation" />
        </div>
    </div>

<script type="text/javascript">
    $(document).ready(function() {
        var dlg = $('#divNewReservation');
        $('.datepicker').datepicker({ duration: '' });
        dlg.dialog({ autoOpen:false, modal: true, width:400 });
        $('#lnkAddReservation').click(function() { dlg.dialog('open'); return false; });
        dlg.parent().appendTo(jQuery("form:first"));
    });
</script>
+1  A: 

This can be a browser behavior not jQuery plugin issue. Have you tried removing the focus programmatically after you open the popup.

$('#lnkAddReservation').click(function () {
    dlg.dialog('open');

    // you may want to change the selector below
    $('input,textarea,select').blur();

    return false;
});

Haven't tested that but should work ok.

RaYell
sadly this doesn't work. not sure if it's an IE thing, or a jquery UI thing. but even if you move the focus to something else programatically, the calendar stays open.
Patricia
+1  A: 

Set the tabindex of the input to -1

redsquare
Won't tabindex=-1 prevent me from tabbing to the textbox?
slolife
u could use a setTimeout to set the tabindex back after the dialog is shown
redsquare
+2  A: 

I found the following code the jQuery UI dialog function for open.

c([]).add(d.find(".ui-dialog-content :tabbable:first")).add(d.find(".ui-dialog-buttonpane :tabbable:first")).add(d).filter(":first").focus();

You can either workaround the jQuery behaviour or change the behaviour.

tabindex -1 works as a workaround.

Flugan
Won't tabindex=-1 prevent me from tabbing to the textbox?
slolife
Thanks for the research. Now I know that it is actual code that is doing it.I might make a suggestion to the jQuery UI team to add an option to disable this auto-focus.
slolife
http://dev.jqueryui.com/ticket/4731
slolife
A: 

I had content that was longer than the dialog. On open, the dialog would scoll to the first :tabbable which was at the bottom. Here was my fix.

$("#myDialog").dialog({
   ...
   open: function(event, ui) { $(this).scrollTop(0); }
});
thetoolman
A: 

You can Add this :

...

dlg.dialog({ autoOpen:false,
modal: true, 
width: 400,
open: function(){                  // There is new line
  $("#txtStartDate").focus();
  }
});

...

F1dz