Uncaught HierarchyRequestError: Failed to execute 'insertBefore' on 'Node': The new child element contains the parent

Rav_g

Uncaught HierarchyRequestError: Failed to execute 'insertBefore' on 'Node': The new child element contains the parent.

I just try to append span9 before span3 when windows width is less than 767 and I have this error, why is that?

enter image description here

$(document).ready(function() {
  // Optimalisation: Store the references outside the event handler:
  var $window = $(window);
  var $content = $('.content');
  var $cats = $('.span3');

  function checkWidth() {
      var windowsize = $window.width();
      if (windowsize < 767) {
        $content.insertBefore($cats);
      } else if (windowsize > 767) {
        $content.insertAfter($cats);
      }
    }
    // Execute on load
  checkWidth();
  // Bind event listener
  $(window).resize(checkWidth);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<div class="content-entry">
  <div class="row">
    <div class="span3">span3</div>
    <div class="span9 content">span9/content</div>
  </div>
</div>

T.J. Crowder

Although the error doesn't occur with what you've shown, the error message is quite clear: Apparently, you have at least one element with the class span3 that's inside an element with the class content, like this:

$(document).ready(function() {
  // Optimalisation: Store the references outside the event handler:
  var $window = $(window);
  var $content = $('.content');
  var $cats = $('.span3');

  function checkWidth() {
      var windowsize = $window.width();
      if (windowsize < 767) {
        $content.insertBefore($cats);
      } else if (windowsize > 767) {
        $content.insertAfter($cats);
      }
    }
    // Execute on load
  checkWidth();
  // Bind event listener
  $(window).resize(checkWidth);
});
<div class=".content-entry">
  <div class="row">
    <div class="content"><!-- Note this new element -->
        <div class="span3">span3</div>
    </div>
    <div class="span9 content">span9</div>
  </div>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

The solution is to target your selectors more carefully at just the elements you want to move around. Apparently content and/or span3 are used for other things as well as what you're using them for here.

For instance:

$(document).ready(function() {
  // Optimalisation: Store the references outside the event handler:
  var $window = $(window);
  var $content = $('.move-this');     // Changed the class
  var $cats = $('.relative-to-this'); // Changed the class

  function checkWidth() {
      var windowsize = $window.width();
      if (windowsize < 767) {
        $content.insertBefore($cats);
      } else if (windowsize > 767) {
        $content.insertAfter($cats);
      }
    }
    // Execute on load
  checkWidth();
  // Bind event listener
  $(window).resize(checkWidth);
});
<div class=".content-entry">
  <div class="row">
    <div class="content"><!-- Note this new element -->
        <div class="span3 relative-to-this">span3</div><!-- Added a class -->
    </div>
    <div class="span9 content move-this">span9</div><!-- Added a class -->
  </div>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Uncaught NotFoundError: Failed to execute 'insertBefore' on 'Node': The node before which the new node is to be inserted is not a child of this node

From Dev

Cannot append DOM element to DIV node: Uncaught HierarchyRequestError: Failed to execute 'appendChild' on 'Node'

From Dev

Uncaught TypeError: Failed to execute 'insertBefore' on 'Node': parameter 1 is not of type 'Node'

From Dev

The new child element contains the parent

From Dev

The new child element contains the parent

From Dev

Failed to execute 'appendChild' on 'Node': The new child element is null

From Dev

Error: Failed to execute 'insertBefore' on 'Node'

From Dev

Uncaught HierarchyRequestError

From Dev

Uncaught TypeError: Failed to execute 'appendChild' on 'Node': parameter 1 is not of type 'Node'

From Dev

NotFoundError: Failed to execute 'removeChild' on 'Node': The node to be removed is not a child of this node

From Dev

typeError: Failed to execute 'contains' on 'Node': parameter 1 is not of type 'Node Polymer

From Dev

Copy parent element that contains specific child element using xslt

From Dev

Check if an span element contains a "text" and is the last child of a parent element

From Dev

Uncaught TypeError: Failed to execute 'getComputedStyle' on 'Window': parameter 1 is not of type 'Element'

From Dev

remove parent node depending on the child element values

From Dev

jQuery Chosen: Uncaught NotFoundError: Failed to execute 'appendChild' on 'Node':

From Dev

execv and fork: inform parent that child failed to execute the file

From Dev

why child element <img /> not contains in the parent div width and height?

From Dev

Trying to execute of a child element inside a parent array java

From Dev

Failed to execute child

From Dev

XSLT - Remove full parent node if grandchild element contains specific text

From Dev

XSLT - Remove full parent node if grandchild element contains specific text

From Dev

Javascript error: Uncaught TypeError: Failed to execute 'removeChild' on 'Node': parameter 1 is not of type 'Node'

From Dev

XElement.Element failed to select the correct child node

From Dev

Get the parent node on the basis of single child element with specific attribute

From Dev

HTML issue: Is it okay if I close a parent element and then a child node?

From Dev

Using XSLT, remove parent element when child element contains a nil true attribute

From Dev

failed to execute appendchild on node

From Dev

Style parent if child contains X

Related Related

  1. 1

    Uncaught NotFoundError: Failed to execute 'insertBefore' on 'Node': The node before which the new node is to be inserted is not a child of this node

  2. 2

    Cannot append DOM element to DIV node: Uncaught HierarchyRequestError: Failed to execute 'appendChild' on 'Node'

  3. 3

    Uncaught TypeError: Failed to execute 'insertBefore' on 'Node': parameter 1 is not of type 'Node'

  4. 4

    The new child element contains the parent

  5. 5

    The new child element contains the parent

  6. 6

    Failed to execute 'appendChild' on 'Node': The new child element is null

  7. 7

    Error: Failed to execute 'insertBefore' on 'Node'

  8. 8

    Uncaught HierarchyRequestError

  9. 9

    Uncaught TypeError: Failed to execute 'appendChild' on 'Node': parameter 1 is not of type 'Node'

  10. 10

    NotFoundError: Failed to execute 'removeChild' on 'Node': The node to be removed is not a child of this node

  11. 11

    typeError: Failed to execute 'contains' on 'Node': parameter 1 is not of type 'Node Polymer

  12. 12

    Copy parent element that contains specific child element using xslt

  13. 13

    Check if an span element contains a "text" and is the last child of a parent element

  14. 14

    Uncaught TypeError: Failed to execute 'getComputedStyle' on 'Window': parameter 1 is not of type 'Element'

  15. 15

    remove parent node depending on the child element values

  16. 16

    jQuery Chosen: Uncaught NotFoundError: Failed to execute 'appendChild' on 'Node':

  17. 17

    execv and fork: inform parent that child failed to execute the file

  18. 18

    why child element <img /> not contains in the parent div width and height?

  19. 19

    Trying to execute of a child element inside a parent array java

  20. 20

    Failed to execute child

  21. 21

    XSLT - Remove full parent node if grandchild element contains specific text

  22. 22

    XSLT - Remove full parent node if grandchild element contains specific text

  23. 23

    Javascript error: Uncaught TypeError: Failed to execute 'removeChild' on 'Node': parameter 1 is not of type 'Node'

  24. 24

    XElement.Element failed to select the correct child node

  25. 25

    Get the parent node on the basis of single child element with specific attribute

  26. 26

    HTML issue: Is it okay if I close a parent element and then a child node?

  27. 27

    Using XSLT, remove parent element when child element contains a nil true attribute

  28. 28

    failed to execute appendchild on node

  29. 29

    Style parent if child contains X

HotTag

Archive