get all src attributes of video source tags in javascript

You can get all the src attributes of source tags inside a video tag using JavaScript by accessing the video element and its source child elements, and then retrieving the src attribute of each source element. Here's how you can do it:

index.tsx
// Retrieve the video element
const videoElement = document.querySelector('video');

// Retrieve all the source child elements of the video element
const sourceElements = videoElement.querySelectorAll('source');

// Retrieve the src attribute of each source element
const srcAttributes = Array.from(sourceElements).map(source => source.getAttribute('src'));

// Log the src attributes to the console
console.log(srcAttributes);
428 chars
12 lines

Here, we first use document.querySelector() to retrieve the video element. Then, we use querySelectorAll() to retrieve all the source child elements of the video element. We then use Array.from() to convert the node list of source elements to an array, and map() to retrieve the src attribute of each source element using the getAttribute() method. Finally, we log the src attributes to the console.

gistlibby LogSnag