What would be the best way to get the IMG SRC to an array such as:
var images = [ ‘img1.png’, ‘img2.png’, img3.png’ ];
<div class="cc">
<img class='content-image' src="./img1.png" alt="">
<img class='content-image' src="./img2.png" alt="">
<img class='content-image' src="./img3.png" alt="">
<img class='content-image' src="./img4.png" alt="">
<img class='content-image' src="./img5.png" alt="">
</div>
Have tried this:
var images = []; const interactImg2 = document.querySelector('.cc'); [...interactImg2.querySelectorAll('img[src]')].forEach(img => images.push(new Image('img[src]')));
Unable to get the SRC but the item shows as ( Image1, Image2, Image3 )
6
Answers
Try this code. If you have any problem further, feel free to reach out me.
You can use a compound CSS selector and a regexp to extract image file names. If you need full image paths, just return
src
without regexing.Use
querySelectorAll
to select all images and either aforEach
loop or amap
to create the array:Try this its 100% working
Use the below code it also works: