Trying to make Alert on all href of the page but only one is displayed

Asked

Viewed 64 times

1

I’m doing some tests and I came across this very boring mistake.

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

<ul>

<li> <a href='http://www.google.com' 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>

Then 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>
  • 1

    tries to change jquery to $('href').each(function(){var href = $('.tooltip').attr('href');&#xA; alert(href);});

1 answer

0


When you select the elements by Class, Jquery will store all occurrences in an array. Therefore, the correct is:

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

    for(var i = 0; i < hrefArray.length; i++){
        alert(hrefArray[i].href);
    }
});

Browser other questions tagged

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