Tuesday, June 4, 2024
 Popular · Latest · Hot · Upcoming
111
rated 0 times [  118] [ 7]  / answers: 1 / hits: 6287  / 4 Years ago, sun, december 13, 2020, 12:00:00

I have a function to play only the audio of a video mp4 file... part of the function is this (for simplicity I just write the error producing part):


const video = document.createElement('video');
document.body.appendChild(video);
video.id = 'audio-clip';
const clip = document.getElementById("audio-clip");
clip.style.visibility = "hidden";
const source = document.createElement('source');
source.src = getBlob(clipSource);
source.type = 'video/mp4';
video.appendChild(source);
video.load();

Then when I want to stop the video I simply remove it from the DOM by this function:


StopMovieAudio = () => {
console.log('clip', clip);
console.log('clip.outerHTML', clip.outerHTML);
clip.outerHTML = "";
}

The issue is sometimes I get an error that ruins the whole code: here is my console:


enter


As you see the weird part of the error is while I can log the clip.outerHTML and it returns the DOM correctly in the very next line I get the unexpected error:



Failed to set the 'outerHTML' property on 'Element': This element has
no parent node.



What have I missed and how to fix this?


Edit: Guys I noticed even more weird thing... the clip is not attached into body ... even though we can log console.log('clip', clip); and it returns the element, there is no such element in console.log(document.body)!!!!!!!


More From » javascript

 Answers
18

I wouldn't recommend using non-standard browser extensions such as outerHTML, but if you insist, outerHTML doesn't work on any elements that have been detached from the DOM that have no parent, or the HTML tag (which is expected to be the root).


I don't know how this applies to your situation at all, there's probably more code that you haven't shown.


Since you're seeking to remove the element, it would be more readable to use the W3C DOM API's Node#remove:


clip.remove();

You can read more about it at MDN.


[#2129] Tuesday, December 8, 2020, 4 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
leifw

Total Points: 88
Total Questions: 103
Total Answers: 103

Location: France
Member since Thu, May 6, 2021
3 Years ago
leifw questions
Wed, Sep 14, 22, 00:00, 2 Years ago
Wed, Sep 1, 21, 00:00, 3 Years ago
Wed, Apr 21, 21, 00:00, 3 Years ago
Sat, Mar 28, 20, 00:00, 4 Years ago
;