diff --git a/misc/ajax.js b/misc/ajax.js index 3b9dec6..a478c48 100644 --- a/misc/ajax.js +++ b/misc/ajax.js @@ -415,6 +415,15 @@ Drupal.ajax.prototype.success = function (response, status) { // Remove any response-specific settings so they don't get used on the next // call by mistake. this.settings = null; + + // If the element that triggered the ajax request is still in the DOM and + // visible, set the focus back to that element. + if (this.form && this.element) { + var $element = this.form.find('[name="' + this.element.name + '"]'); + if ($element.length !== 0 && $element.is(':visible')) { + $element.focus(); + } + } }; /** diff --git a/modules/file/file.module b/modules/file/file.module index a4a7f03..0ef927e 100644 --- a/modules/file/file.module +++ b/modules/file/file.module @@ -272,6 +272,7 @@ function file_ajax_upload() { } // Add the special Ajax class if a new file was added. + $new_content_id = $form['#id'] . '-ajax-new-content'; if (isset($form['#file_upload_delta']) && $current_file_count < $form['#file_upload_delta']) { $form[$current_file_count]['#attributes']['class'][] = 'ajax-new-content'; } @@ -279,12 +280,15 @@ function file_ajax_upload() { else { $form['#suffix'] .= ''; } + $form['#prefix'] .= ''; $output = theme('status_messages') . drupal_render($form); $js = drupal_add_js(); $settings = call_user_func_array('array_merge_recursive', $js['settings']['data']); $commands[] = ajax_command_replace(NULL, $output, $settings); + $commands[] = ajax_command_invoke('#' . $new_content_id, 'attr', array('tabIndex', '-1')); + $commands[] = ajax_command_invoke('#' . $new_content_id, 'focus'); return array('#type' => 'ajax', '#commands' => $commands); }