Error: Failed to execut 'removeChild' on 'Node'

RandomGuy

I'm new to javascript and programming in general. I've put together my first browser game and while everything functions, I keep getting this pesky error in the console that says, "NotFoundError: Failed to execute 'removeChild' on 'Node': The node to be removed is not a child of this node" in my hover function.

function hover(myid) {
var id = myid;
var index = id[5];
document.getElementById(id).removeChild(GAME.mound[index]);
document.getElementById(id).appendChild(GAME.hover[index]);
GAME.sound[0].play();
}

Both GAME.mound and GAME.hover are arrays that store images. The thing I can't figure out is that I have a function that is nearly identical to reverse the image switch caused by the hover function, but it doesn't throw the error.

function out(myid) {
var id = myid;
var index = id[5];
document.getElementById(id).removeChild(GAME.hover[index]);
document.getElementById(id).appendChild(GAME.mound[index]);
}

Here is a line from the HTML that calls the functions:

<div id="drift0" class="snowdrift" onmouseover = "hover(this.id)" onmouseout = "out(this.id)" onclick = "popup(this.id)"></div>

Any help in this is greatly appreciated. Let me know if I didn't provide all the information needed to answer the question.

Thanks!

RobG

It seems you need to handle this exception, so do that. There doesn't seem to be any point to copying myid, it's a string and even if you modify it, the original isn't changed. You should pass a reference to the element to the listener rather than the ID, as it saves using getElementById twice.

Also, as Deryck suggested, replaceChild will do the change in one step:

function hover(myid) {
  var el = document.getElementById(myid);
  var index = myid[5];

  if (GAME.mound[index].parentNode === el) {
    // Replace mound image with hover image
    e.replaceChild(GAME.hover[index], GAME.mound[index]);
  }
  GAME.sound[0].play();
}

If you pass this to the function rather than this.id, you could do:

function hover(el) {
  var index = el.id[5];

  if (GAME.mound[index].parentNode === el) {
    e.replaceChild(GAME.hover[index], GAME.mound[index]);
  }
  GAME.sound[0].play();
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

MongoDB setup with node.js: Error: failed to connect to [localhost:27017]

From Dev

Docker - Node.js + MongoDB - "Error: failed to connect to [localhost:27017]"

From Dev

Error installing contextify- node-gyp rebuild failed

From Dev

jQuery .remove() vs Node.removeChild() and external maps to DOM nodes

From Dev

RemoveChild when node is empty

From Dev

Node.js npm install express error fetch failed

From Dev

NotFoundError Node was not found image_div.parentNode.removeChild(img)

From Dev

Need run twice Node.removeChild() for node removed

From Dev

Why Node.removeChild(Old Child) doesn't works in functions?

From Dev

Getting an error: Failed to execute 'appendChild' on 'Node': parameter 1 is not of type 'Node'

From Dev

Error node.js Failed to load resource

From Dev

Node Express ejs Error: Failed to lookup view "error" in views directory

From Dev

erlang: failed to spawn(node, fun): badfun error

From Dev

Leaflet.markercluster onclick error - Failed to execute 'appendChild' on 'Node': parameter 1 is not of type 'Node'

From Dev

Node Child Process Exec Command Failed with error code 1

From Dev

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

From Dev

Failed to execute 'removeChild' on 'Node': parameter 1 is not of type 'Node'

From Dev

Node.js error : failed to initialize context

From Dev

Getting "message":"Admin validation failed" Error in mongoose, node.js

From Dev

Failed to allocate nodeid, error: 'Error: Could not alloc node id

From Dev

simple node.js server not error handling failed request

From Dev

Failed to execute 'removeChild'

From Dev

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

From Dev

Passport Node(Error: Failed to deserialize user out of session.)

From Dev

Javascript DOM Manipulation error with removeChild and addEventListener

From Dev

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

From Dev

RemoveChild error for some id

From Dev

RemoveChild does not work despite there is no error

From Dev

Node startup failed with error: internal.Node.run - Exception during node startup {} in Corda

Related Related

  1. 1

    MongoDB setup with node.js: Error: failed to connect to [localhost:27017]

  2. 2

    Docker - Node.js + MongoDB - "Error: failed to connect to [localhost:27017]"

  3. 3

    Error installing contextify- node-gyp rebuild failed

  4. 4

    jQuery .remove() vs Node.removeChild() and external maps to DOM nodes

  5. 5

    RemoveChild when node is empty

  6. 6

    Node.js npm install express error fetch failed

  7. 7

    NotFoundError Node was not found image_div.parentNode.removeChild(img)

  8. 8

    Need run twice Node.removeChild() for node removed

  9. 9

    Why Node.removeChild(Old Child) doesn't works in functions?

  10. 10

    Getting an error: Failed to execute 'appendChild' on 'Node': parameter 1 is not of type 'Node'

  11. 11

    Error node.js Failed to load resource

  12. 12

    Node Express ejs Error: Failed to lookup view "error" in views directory

  13. 13

    erlang: failed to spawn(node, fun): badfun error

  14. 14

    Leaflet.markercluster onclick error - Failed to execute 'appendChild' on 'Node': parameter 1 is not of type 'Node'

  15. 15

    Node Child Process Exec Command Failed with error code 1

  16. 16

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

  17. 17

    Failed to execute 'removeChild' on 'Node': parameter 1 is not of type 'Node'

  18. 18

    Node.js error : failed to initialize context

  19. 19

    Getting "message":"Admin validation failed" Error in mongoose, node.js

  20. 20

    Failed to allocate nodeid, error: 'Error: Could not alloc node id

  21. 21

    simple node.js server not error handling failed request

  22. 22

    Failed to execute 'removeChild'

  23. 23

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

  24. 24

    Passport Node(Error: Failed to deserialize user out of session.)

  25. 25

    Javascript DOM Manipulation error with removeChild and addEventListener

  26. 26

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

  27. 27

    RemoveChild error for some id

  28. 28

    RemoveChild does not work despite there is no error

  29. 29

    Node startup failed with error: internal.Node.run - Exception during node startup {} in Corda

HotTag

Archive