Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get img src with JSDOM (Node.js)

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?

like image 913
Nackr Avatar asked Oct 31 '25 15:10

Nackr


1 Answers

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()

like image 162
abhishekkannojia Avatar answered Nov 02 '25 03:11

abhishekkannojia



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!