Monolithic Monolithic - 1 year ago 92
Javascript Question

Change <img> src according to it's alt attribute

How to change an image src according to it's unique alt attribute by using a for loop targeted by document.querySelectorAll(img) using vanilla JS. Ty


<img src="../../img/potatoes.png" alt="potatoes">


<img src="../../img/potatoes/newpotatoes.png" alt="potatoes">

Answer Source
  img.src = img.src.replace('.png', '/new' + img.alt.charAt(0).toUpperCase() + img.alt.slice(1) + '.png')
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download