It looks like calling focus()
(which jquery-ui uses the first tabbable element by default) in Chrome (cannot test IE - on OS X) focuses the field and selects the text in the field.
Taken from jquery.dialog.ui.js :
// set focus to the first tabbable element in the content area or the first button // if there are no tabbable elements, set focus on the dialog itself $(self.element.find(':tabbable').get().concat( uiDialog.find('.ui-dialog-buttonpane :tabbable').get().concat( uiDialog.get()))).eq(0).focus();
Firefox, on the other hand, seems to place the cursor in the field when the focus is invoked. Therefore, you must invoke an implicit select call after the dialog has been created to achieve what you want to do.
If you reload the timer script (as opposed to clicking), you will notice that the example works every time. I think jsFiddle is actually the culprit here (maybe a hashchange event or some kind of focal event on one of the panels after clicking the "run" button - I didnβt dig that deep).
EDIT : (sorry, this is late) It seems the main cause of the βproblemβ is Firefox. I'm not sure if this is a designed behavior or not, but from what I see, it seems that Firefox will not allow you to select text in two different input elements in different areas of content simultaneously on the same page. This does not seem to affect Chrome (and, presumably, IE9).
I made a quick example locally that has two iframes side by side (let me call them left and right). There is a text box on the left and your jquery-ui dialog is on the right, similar to the script you posted. right has the following code:
<script type="text/javascript"> $('<div><input type="text" value="val" /></div>').dialog(); $('input').select(); </script>
left has the following code:
<script type="text/javascript"> setTimeout( function() { $('textarea').focus(); }, 1000); </script>
If you combine them and see the result in Firefox, you will notice that the input is focused and selected until the text area in the left is focused. I suspect something like this is happening in jsFiddle.