13

The first element in my jQuery UI Dialog is an input, which when selected opens a datepicker...

How can I disable this input from being selected first?

Starx
  • 77,474
  • 47
  • 185
  • 261
Josh
  • 605
  • 2
  • 8
  • 16

3 Answers3

47

Add the following code before you call dialog. This will clear out the autofocus code. It works for me in jquery 2.0.3.

$.ui.dialog.prototype._focusTabbable = function(){};
LaXDragon
  • 631
  • 5
  • 5
9

Very simple, just trigger the blur event on the input elements when the dialog box opens.

$("#dialog").dialog({
    open: function(event, ui) {
        $("input").blur();
    }
});

Check it out here

Solution with datepicker

NOTE: For more in-depth solution to this problem, read this answer too.

Community
  • 1
  • 1
Starx
  • 77,474
  • 47
  • 185
  • 261
  • 2
    Unfortunately this doesn't work when dealing with a dialog that has autoOpen: false. http://jsfiddle.net/8VbEU/134/ – Wulfhart Mar 28 '14 at 19:04
5

JQuery sets the autofocus on the first input that is found.
So play it sneaky by creating a "fake" input at the first line of your dialog like that:

<input type='text' size='1' style='position:relative;top:-500px;' />

So your input will be out of the window and have the focus. Problem solved for me ;p

Michael Myers
  • 188,989
  • 46
  • 291
  • 292
kts
  • 77
  • 1
  • 1