The issue is because of jQuery UI Dialog's built in autofocus function:
You can prevent this by executing this function before you call dialog.
$.ui.dialog.prototype._focusTabbable = function(){};
Which redefines the focus functionality to be an empty function.
Your example with the added code: