diff -u b/core/misc/ajax.js b/core/misc/ajax.js --- b/core/misc/ajax.js +++ b/core/misc/ajax.js @@ -1034,10 +1034,10 @@ var $responseDataWrapped = $('
').html(response.data); var $new_content = $('
'); - var elementsReturned = $responseDataWrapped.contents().length; + var elementsReturnedCount = $responseDataWrapped.contents().length; // If only one node element is returned skip wrap processing. - if (elementsReturned === 1 && response.data === Node.ELEMENT_NODE) { + if (elementsReturnedCount === 1 && response.data === Node.ELEMENT_NODE) { $new_content = response.data; } else { @@ -1067,7 +1067,7 @@ // There are no other elements to process anymore, so add the // intermediate wrapper to the content if present. - if (index === elementsReturned - 1 && $intermediateWrapper) { + if (index === elementsReturnedCount - 1 && $intermediateWrapper) { $new_content.append($intermediateWrapper); } }); @@ -1114,10 +1114,15 @@ if ($new_content.parents('html').length > 0) { // Apply any settings from the returned JSON if available. settings = response.settings || ajax.settings || drupalSettings; - // Attach behaviors to all element nodes. - $wrapper.children().each(function () { - Drupal.attachBehaviors(this, settings); - }); + if (method === 'replaceWith') { + Drupal.attachBehaviors($wrapper.get(0), settings); + } + else { + // Attach behaviors to all element nodes. + $wrapper.children().each(function () { + Drupal.attachBehaviors(this, settings); + }); + } } },