jQuery is only returning first item

Posted by Andy on Stack Overflow See other posts from Stack Overflow or by Andy
Published on 2010-05-13T15:46:49Z Indexed on 2010/05/13 15:54 UTC
Read the original article Hit count: 140

Filed under:
|

For some strange reason, whenever I have a selector and expect to get multiple items, jQuery is only returning the first item, instead of the entire collection.

This is the HTML I have:

<a id="reply-424880" class="reply" href="#" rel="nofollow">Reply</a>
<a id="reply-424885" class="reply" href="#" rel="nofollow">Reply</a>

And the selector:

$('.reply').unbind('click').click(function(event) {
 ...
}

I have tried debugging using FireBug, and still get the same results. Using the work around I can get it to work:

$('a').each(function (index, element) {
            if ($(element).attr('class') == 'reply') {
                $(this).unbind('click').click(function(event) {
                     ...
                });
             }
});

I would like to use the built-in functionality instead of my work around. Any idea why only the first element would be returned?

© Stack Overflow or respective owner

Related posts about jQuery

Related posts about jquery-selectors