I want to use javascript to automatically update my index.html doc based on files which are inside the images directory.
Here's a snippet of how the html should look if the directory contained the following 3 image files 'moth_forsale.jpg', 'art_sold.jpg', 'bat_forsale.jpg'.
<a class='gallery-link' target="_blank" href='../images/moth_forsale.jpg'>
<img src="../images_small/moth_forsale.jpg" class="all forsale">
</a>
<a class='gallery-link' target="_blank" href='../images/art_sold.jpg'>
<img src="../images_small/art_sold.jpg" class="all forsale">
</a>
<a class='gallery-link' target="_blank" href='../images/bat_forsale.jpg'>
<img src="../images_small/bat_forsale.jpg" class="all forsale">
</a>
As shown, in addition to updating the file paths, I also need to update the class, with either 'forsale' or 'sold', depending on what the image name contains.
try this:
if you need to check and remove previous classes, you can use
img.classList.remove()
method.If you only need to address the image in your CSS you can use partial attribute CSS selector:
use it to get all images with
forsale
string intheirsrc
attribute, andfor images with
sold