I am new to javascript. I am trying to loop throw all images and check if there is an alt property and add empty alt="" to images that don't have one.
var images =document.querySelectorAll(".view.view-mt-latest-news.view-id-mt_latest_news.view-display-id-block_4 > div > div > div > div > a > img ");
for (var i = 0, image; image = images[i]; i++) {
image.alt = "";
}
<div class="view view-mt-latest-news view-id-mt_latest_news view-display-id-block_4 view-dom-id-16ee052fc9069895592eb40c1695ac6f jquery-once-2-processed">
<div class="view-content">
<div class="views-row views-row-1 views-row-odd views-row-first clearfix">
<div class="views-field views-field-field-image">
<div class="field-content overlayed">
<a href="#"><img typeof="foaf:Image" src="https://via.placeholder.com/150" width="89" height="75"></a></div>
</div>
</div>
<div class="views-row views-row-1 views-row-odd views-row-first clearfix">
<div class="views-field views-field-field-image">
<div class="field-content overlayed">
<a href="#"><img typeof="foaf:Image" src="https://via.placeholder.com/150" width="89" height="75" alt="This is alt"></a></div>
</div>
</div>
<div class="views-row views-row-1 views-row-odd views-row-first clearfix">
<div class="views-field views-field-field-image">
<div class="field-content overlayed">
<a href="#"><img typeof="foaf:Image" src="https://via.placeholder.com/150" width="89" height="75"></a></div>
</div>
</div>
</div>
</div>
This code adds alt to all images like this:
<img typeof="foaf:Image" src="https://via.placeholder.com/150" width="89" height="75" alt>
It should be like this:
<img typeof="foaf:Image" src="https://via.placeholder.com/150" width="89" height="75" alt="">
Can anyone help with this for loop.