I need to find the src of the img tag in my string:
const dom = new JSDOM('<img src="godaddy.com">',
{ includeNodeLocations: true });
console.log(dom.window.document.querySelector("img"));
But the console output is:
HTMLImageElement {}
How can I get the value of the src attribute?
I've not used the jsdom myself, but assuming it pretty much has same api's as browser dom. You could be able to use this:
const dom = new JSDOM('<img src="godaddy.com">', { includeNodeLocations: true });
console.log(dom.window.document.querySelector("img").getAttribute('src');
If you have multiple images then you can use querySelectorAll() which returns a NodeList object representing the set of matched elements. You can then loop through this list and get the src values.
More about querySelectorAll()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With