Django Django - 7 months ago 18
Javascript Question

Jquery: Get 'title' attribute of each element, not just the first

I am trying to display the title attribute of each element with the class "tooltip", using the jQuery plugin Tooltipster.

But for whatever reason, I just get the "title" of the first element - for every element on the page.

<i class="tooltip game-1" title="219"></i>
<i class="tooltip game-2" title="30"></i>
<i class="tooltip game-3" title="122"></i>


$(document).ready(function() {
$('.tooltip').tooltipster({
content: $(''+$(this).attr('title')+' people played this game')
});
});


I just get 219 over and over.

Answer

Try using each() to apply tooltipster() to each .tooltip element seperately.

 $(document).ready(function() {
    $('.tooltip').each(function(){
        $(this).tooltipster({
           content: $(''+$(this).attr('title')+' people played this game')
        });
    });
});