tags:

views:

948

answers:

4

Hi,
when a jQuery UI dialog opens it seems to select one of the buttons and highlights it or sets focus to it etc... How can I stop this behaviour so that none of the buttons are highlighted when the dialog opens?
Thanks!

EDIT
I tried the following in the dialog options which didn't remove focus from the buttons

...
open:function(event, ui) { $("myunimportantdiv").focus(); },
...

NOTE
as a temporary workaround I modified the css for ui-state-focus but this isn't ideal...

A: 

Set the focus to some other element...

$(document).ready(function() {
    $("#myunimportantdiv").focus();
});
jjclarkson
I tried putting open: function(event, ui) { $("myunimportantdiv").focus(); }, in the dialog declaration as I figured that code would run when it opened but the button still had focus...
davidsleeps
Did you create some element to send the focus to such as a div? My intention was that you would have <div id="myunimportantdiv""></div> in your markup somewhere that could retake the focus.I noticed also in your edit that you removed the "#" designating ID.
jjclarkson
good point, will check that!
davidsleeps
Still didn't work for me (the div I was using was on the page) but the order of events in the open function is what is causing the problem...
davidsleeps
+1  A: 

Use the blur method. You can try this sample.

<html>
    <head>
        <title>No Focus on jQuery Dialog</title>
        <link type="text/css" rel="stylesheet" href="ui.all.css" />
        <script type="text/javascript" src="jquery-1.3.2.min.js"></script>
        <script type="text/javascript" src="ui.core.js"></script>
        <script type="text/javascript" src="ui.dialog.js"></script>
        <script type="text/javascript">
            $(document).ready(function() {
                // Dialog to confirm or cancel
                // Focuses on confirm by default.
                $('#noFocusDialog').dialog({
                    autoOpen: false,
                    buttons: {
                        Confirm: function() {
                            $(this).dialog('close');
                        },
                        Cancel: function() {
                            $(this).dialog('close');
                        }
                    }
                });

                // Trigger to open the dialog
                $('#openNoFocusDialog').click(function() {
                    $('#noFocusDialog').dialog('open');

                    // Remove focus on all buttons within the
                    // div with class ui-dialog
                    $('.ui-dialog :button').blur();
                });
            });
        </script>
    </head>
    <body>
        <a id="openNoFocusDialog" href="#">Open Dialog</a>
        <div id="noFocusDialog">
            <p>Confirm that no elements have focus</p>
        </div>
    </body>
</html>
Ed Saito
A: 

This feature (or bug ?) is no longer available in jQuery UI 1.8, update your version !

Scorpi0
Thanks for posting this
davidsleeps
A: 

I use JQuery Ui 1.8.4 and it still has this problem but not all the time. Anyone has the same issue?

I have the same problem with JQuery UI 1.8.4 and testing in FF3. I am using the buttons to open dialogs so I am currently using .blur before any dialog close call.
jarkam