diff --git a/core/misc/ajax.es6.js b/core/misc/ajax.es6.js
index 8c59d13989..959c3565f2 100644
--- a/core/misc/ajax.es6.js
+++ b/core/misc/ajax.es6.js
@@ -989,7 +989,10 @@
    * @param {object} response
    *   The response from the Ajax request.
    *
-   * @todo: Mark the function as deprecated after it will be possible. See
+   * @deprecated in Drupal 8.5.x and will be removed before Drupal 9.0.0.
+   * Use data with desired wrapper.
+   *
+   * @todo: Add trigger an error after it will be possible. See
    * https://www.drupal.org/project/drupal/issues/2973400
    */
   Drupal.theme.ajaxWrapperNewContent = ($newContent, ajax, response) => (
@@ -1012,7 +1015,10 @@
    * @param {jQuery} $elements
    *   Response elements after parsing.
    *
-   * @todo: Mark the function as deprecated after it will be possible. See
+   * @deprecated in Drupal 8.5.x and will be removed before Drupal 9.0.0.
+   * Use data with desired wrapper.
+   *
+   * @todo: Add trigger an error after it will be possible. See
    * https://www.drupal.org/project/drupal/issues/2973400
    */
   Drupal.theme.ajaxWrapperMultipleRootElements = ($elements) => (
@@ -1076,8 +1082,10 @@
       // Apply any settings from the returned JSON if available.
       const settings = response.settings || ajax.settings || drupalSettings;
 
-      // Parse response.data to get element collection.
-      let $newContent = $('<div></div>').html(response.data).contents();
+      // Parse response.data into an element collection.
+      let $newContent = $($.parseHTML(response.data, document, true));
+      // For backward compatibility, in some cases a wrapper will be added. Use
+      // theming to change it. See https://www.drupal.org/node/2940704.
       $newContent = Drupal.theme('ajaxWrapperNewContent', $newContent, ajax, response);
 
       // If removing content from the wrapper, detach behaviors first.
diff --git a/core/misc/ajax.js b/core/misc/ajax.js
index 2011e2e63f..3506c56a96 100644
--- a/core/misc/ajax.js
+++ b/core/misc/ajax.js
@@ -497,7 +497,8 @@ function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr
 
       var settings = response.settings || ajax.settings || drupalSettings;
 
-      var $newContent = $('<div></div>').html(response.data).contents();
+      var $newContent = $($.parseHTML(response.data, document, true));
+
       $newContent = Drupal.theme('ajaxWrapperNewContent', $newContent, ajax, response);
 
       switch (method) {
