Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javascript removechild

Tags:

javascript

I am trying to delete a hidden element. I have tried the following codes but it always returns the parent as undefined.

attempt one

var timeLeft = document.getElementById("time");
timeLeft.document.removeChild(timeLeft);

attempt two

var timeLeftBody, timeLeft;
timeLeftBody = document.getElementsByTagName("body")[0];
timeLeft = document.getElementById("time");
timeLeft.timeLeftBody.removeChild(timeLeft);
like image 982
Daniel Avatar asked Sep 10 '26 07:09

Daniel


1 Answers

removeChild() only works on an element that directly contains the child -- document.removeChild(hdr) won't work unless the document directly contains the time element (which is impossible, unless time is the html node).

Try:

var timeLeft = document.getElementById("time");
timeLeft.parentNode.removeChild(timeLeft);

Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!