mercredi 5 septembre 2018

else condition is not working for src attribute in jquery

Been trying add alt text in the web page using jquery below code however able to get correct value for the 1st image but else if and else conditions are not working properly. Not able to add alt texts for the other images

$(document).ready(function() {

  $("img ").each(function() {
    var img = $(this);

    if (!img.attr("alt") || img.attr("src") === "/temp/iconcache/flags/32x32/flag_brazil.png")
      img.attr("alt", "Brazil Flag");
    else if (!img.attr("alt") || img.attr("src") === "/temp/iconcache/flags/32x32/flag_China.png")
      img.attr("alt", "China Flag");
    else {
      img.attr("alt", "");
    }
  });
});

Aucun commentaire:

Enregistrer un commentaire