diff --git a/core/misc/ajax.js b/core/misc/ajax.js
index c476f204914c0d15d8ae458839034ac08cf872ee..fe1f8a1845cfe16dfaaef17c0afad96761f34fce 100644
--- a/core/misc/ajax.js
+++ b/core/misc/ajax.js
@@ -1377,17 +1377,18 @@
         $newContent[effect.showEffect](effect.showSpeed);
       }
 
-      // Attach all JavaScript behaviors to the new content, if it was
-      // successfully added to the page, this if statement allows
-      // `#ajax['wrapper']` to be optional.
-      if ($newContent.parents('html').length) {
-        // Attach behaviors to all element nodes.
-        $newContent.each((index, element) => {
-          if (element.nodeType === Node.ELEMENT_NODE) {
-            Drupal.attachBehaviors(element, settings);
-          }
-        });
-      }
+      // Attach behaviors to all element nodes.
+      $newContent.each((index, element) => {
+        if (
+          element.nodeType === Node.ELEMENT_NODE &&
+          // Attach all JavaScript behaviors to the new content, if it was
+          // successfully added to the page, this condition allows
+          // `#ajax['wrapper']` to be optional.
+          document.documentElement.contains(element)
+        ) {
+          Drupal.attachBehaviors(element, settings);
+        }
+      });
     },
 
     /**
diff --git a/core/modules/editor/js/editor.js b/core/modules/editor/js/editor.js
index b0900e99b61448b3a813084824a17b3514db2269..da98baef34055475037cc7e0b52975cb76601205 100644
--- a/core/modules/editor/js/editor.js
+++ b/core/modules/editor/js/editor.js
@@ -241,7 +241,7 @@
           $this.on('change.editorAttach', { field }, onTextFormatChange);
         }
         // Detach any editor when the containing form is submitted.
-        $this.parents('form').on('submit', (event) => {
+        $(field.form).on('submit', (event) => {
           // Do not detach if the event was canceled.
           if (event.isDefaultPrevented()) {
             return;
diff --git a/core/modules/settings_tray/js/settings_tray.js b/core/modules/settings_tray/js/settings_tray.js
index 69e4d85deee6d1f958ef4c9abf45e8f2e83099ba..70e5df5a40a9e6a48a304ba07f1c3fe625319c07 100644
--- a/core/modules/settings_tray/js/settings_tray.js
+++ b/core/modules/settings_tray/js/settings_tray.js
@@ -160,15 +160,15 @@
        * set active editable ID.
        */
       .forEach((instance) => {
+        const closestSettingsTray = instance.element.closest(
+          '.settings-tray-editable',
+        );
         // Check to make sure existing dialogOptions aren't overridden.
         if (!instance.options.data.hasOwnProperty('dialogOptions')) {
           instance.options.data.dialogOptions = {};
         }
-        instance.options.data.dialogOptions.settingsTrayActiveEditableId = $(
-          instance.element,
-        )
-          .parents('.settings-tray-editable')
-          .attr('id');
+        instance.options.data.dialogOptions.settingsTrayActiveEditableId =
+          closestSettingsTray.id;
         instance.progress = { type: 'fullscreen' };
       });
   }