Drew Bartlett
Drew Bartlett

Reputation: 1891

How to use select parent > child with jquery parent() function

I need to select the parent div then child of that with a certain class to append data too when i link is clicked. Please let me know if this makes sense. This makes an ajax call and returns data and needs to append the number to the div called 'num' but I can't seem to select it right

$('.add_fav').live('click', function(){
        var link = $(this);
        var div = $(this).parent().parent();

        // this is where the problem is
        var num = $(this).parent().$('.favnumholder');

        $.get($(this).attr('href'),{}, function(data){
        var data = data.split(':');
            if(data[0] == 'added'){
                div.addClass("hasclass");
                link.addClass('favorited');
                link.attr('href', 'ajax/add-fav.php?action=rm&fav_type='+data[2]+'&fav_id='+data[1]);
                link.text('Un-fav');
                num.html(data[3]);
            } else if(data[0] == 'removed') {
                div.removeClass("hasclass");
                link.removeClass('favorited');
                link.attr('href', 'ajax/add-fav.php?action=add&fav_type='+data[2]+'&fav_id='+data[1]);
                link.text('Fav');
            }
        });
        return false;
    }); 

Upvotes: 2

Views: 1673

Answers (2)

ThiefMaster
ThiefMaster

Reputation: 318508

var num = $(this).parent().find('.favnumholder');

or

var num = $('.favnumholder', $(this).parent());

Upvotes: 3

PeeHaa
PeeHaa

Reputation: 72662

Do you mean:

var num = $('.favnumholder', $(this).parent());

This selects favnumholder in the parent element

Upvotes: 2

Related Questions