André Martins André Martins - 1 year ago 98
Javascript Question

Get all ALT tags

I want to get the alt tags of each image inside a div and list them in a

, but I don't want repeated alts. For example, if 2 imgs have the alt "example1", I want it listed only one time on my

My current code:


$('.img-produto > img').each(function(){
$('.filters').append('<li>'+ $(this).attr('alt') +'</li>');


<ul class="filters"></ul>
<div class="img-produto">
<img src="img link" alt="example1" />
<img src="img link" alt="example1" />


Answer Source

Need to create collection of unique alt values and check that before appending anything

var alts = {};
$('.img-produto > img').each(function(){
  var alt = $(this).attr('alt');
      alts[alt] = true;
      $('.filters').append('<li>'+ alt  +'</li>');
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download