65829

Uncaught DOMException: Failed to execute 'removeChild' on 'Node' [duplicate]

Question:

This question already has an answer here:

<ul><li> <a href="/questions/750486/javascript-closure-inside-loops-simple-practical-example" dir="ltr" rel="nofollow">JavaScript closure inside loops – simple practical example</a> <span class="question-originals-answer-count"> 39 answers </span> </li> </ul> <blockquote>

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

</blockquote>

I'm getting the error when I execute this code below. Is there any ways to solve this problem?

function clickLinks(links) { for(var item in links) { var anchor = document.createElement("a"); anchor.target = "_blank"; anchor.href = links[item]; document.body.appendChild(anchor); window.setTimeout(function() { anchor.dispatchEvent(new MouseEvent("click",{ "bubbles" : true, "cancelable" : true, "view" : window })); window.setTimeout(function() { document.body.removeChild(anchor); }, 50); }, 50); } }

Answer1:

You need to create a closure for the anchor variable you are using in order to ensure that it is not overwritten in the next iteration of the for loop.

function clickLinks(links) { for(var item in links) { var anchor = document.createElement("a"); anchor.target = "_blank"; anchor.href = links[item]; document.body.appendChild(anchor); (function iifeclosure(anchor){ window.setTimeout(function() { anchor.dispatchEvent(new MouseEvent("click",{ "bubbles" : true, "cancelable" : true, "view" : window })); window.setTimeout(function() { document.body.removeChild(anchor); }, 50); }, 50); })(anchor); } }

Recommend

  • Microsoft Chart Controls for Microsoft .NET Framework 4.0
  • Is mp4 stream able with ffserver?
  • Copying rows in a database when rows have children
  • Passing information to server-side function in a Google Docs Add On
  • Python/Javascript: WYSIWYG html editor - Handle large documents fast and/or design theory
  • Create function that can pass a parameter without making a new component
  • Why doesnt this Java loop in a thread work?
  • IE10 strips out hashtag from the URL
  • Alamofire and Reachability.swift not working on xCode8-beta5
  • How can we prepend rows to a react native list-view?
  • uml Composition relationships to RDF and OWL
  • Javascript, Regex - I need to grab each section of a string contained in brackets
  • Tamper-proof configuration files in .NET?
  • Install PHP intl extension on MacOS
  • Xamarin Forms - UWP Fonts
  • Can Jackson SerializationFeature be overridden per field or class?
  • Running a C# exe file
  • Join two tables and save into third-sql
  • How to model a transition system with SPIN
  • Updated Ionic CLI but shows previous version (Windows)
  • Javascript + PHP Encryption with pidCrypt
  • ORA-29908: missing primary invocation for ancillary operator
  • WOWZA + RTMP + HTML5 Playback?
  • How to delete a row from a dynamic generate table using jquery?
  • Rails 2: use form_for to build a form covering multiple objects of the same class
  • NSLayoutConstraint that would pin a view to the bottom edge of a superview
  • Acquiring multiple attributes from .xml file in c#
  • using HTMLImports.whenReady not working in chrome
  • Hits per day in Google Big Query
  • FormattedException instead of throw new Exception(string.Format(…)) in .NET
  • How to CLICK on IE download dialog box i.e.(Open, Save, Save As…)
  • How does Linux kernel interrupt the application?
  • Linking SubReports Without LinkChild/LinkMaster
  • Authorize attributes not working in MVC 4
  • How can I remove ASP.NET Designer.cs files?
  • EntityFramework adding new object to nested object collection
  • XCode 8, some methods disappeared ? ex: layoutAttributesClass() -> AnyClass
  • Converting MP3 duration time
  • java string with new operator and a literal
  • How to push additional view controllers onto NavigationController but keep the TabBar?