Trying to make alert on every page href but only one is displayed

1

I'm doing some testing and I came across one this very annoying error.

It's easy to explain: I want JQuery to give alert to all <a> of the page, but it only alert once and nothing else. Let's go to the codes:

<ul>

<li> <a href='http://www.google.com.br' class='tooltip'> Não clique </a></li>

<li> <a href='http://www.youtube.com.br' class='tooltip'> Não clique </a></li>

<li> <a href='http://www.redtube.com.br' class='tooltip'> Não clique </a></li>

</ul>

So I did JQuery, which is simple:

<script>

/*$(document).ready(function(e) {

      var href = $('.tooltip').attr('href');
    alert(href);
});*/


$(window).scroll(function(){
    var href = $('.tooltip').attr('href');
    alert(href);

    })

</script>
    
asked by anonymous 21.02.2016 / 21:18

1 answer

0

When you select the elements by Class, Jquery will store all occurrences in a vector. So the correct one is:

$(window).scroll(function(){
    var hrefArray = $('.tooltip');

    for(var i = 0; i < hrefArray.length; i++){
        alert(hrefArray[i].href);
    }
});
    
21.02.2016 / 21:35