Take the value of an image attribute and show it below

Asked

Viewed 2,956 times

3

I have this code:

<script>
$(function(){
  $(".flipster").flipster({style: 'carousel', enableTouch: true, start: 0,
    onItemSwitch: function(){
      var nomeAlbum = $(this).attr("data-title");
      console.log(nomeAlbum);
    }
  }); 
});
</script>

The function onItemSwitch() allows me to do anything when the image is changed.
The problem is I’m trying to catch the data-titulo of each of the images, and put it below the photo of the album.

HTML

<ul>
  <li>
    <img src="img/californication.jpg" data-title="Californication" class="imgCarousel"/>
    <p class="mostra-titulo"></p>
  </li>
  <li>
    <img src="img/greatest-hits.jpg" data-title="Greatest Hits" class="imgCarousel" />
    <p class="mostra-titulo"></p>
  </li>
  <li>
    <img src="img/stadium-arcadium.jpg" data-title="Stadium Arcadium" class="imgCarousel" />
    <p class="mostra-titulo"></p>
  </li>
  <li>
    <img src="img/the-uplift-mofo-party-plan.jpg" data-title="The Upflit Mofo Party Plan" class="imgCarousel"/>
    <p class="mostra-titulo"></p>
  </li>
</ul>

Here is the plugin repository, if it helps at all.

1 answer

3


Try it like this:

// iterar todos os li
$('li').each(function() {

    // fazer cache do objecto para evitar funções a mais a correr 
    // como o @Zuul referiu nos comentários
    var $this = $(this); 

    // dentro de cada li, procurar o elemento img e
    // capturar o titulo unsando o método data() do jQuery
    var titulo = $this.find('img').data('title'); 

    // procurar elementos com a classe 'mostra-titulo' e inserir o titulo no seu HTML
    $this.find('.mostra-titulo').html(titulo); 
});

Example

  • 1

    It worked perfectly Sergio!! Thank you very much! D

  • @Luandavi, you’re welcome. Glad I could help.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.