dungey_140
dungey_140

Reputation: 2802

Get .text() value when using nested $(this) statements

When clicking an <a> tag, I want to take the text value of said link and place it inside of another <div> (.current-selection) within the parent. However, I also need to fadeOut() the parent to allow this change to happen out of sight, before bringing it back with fadeIn(). I can get this to work individually, but not when placing the text change within the fadeOut() function.

The code below gets the text of the parent, rather than the originally clicked tag. What can I do differently when nesting like this?

Thank you.

$('.filter').on( 'click', 'a', function() {
    // Fadeout text to allow for hidden text change
    $(this).parent.fadeOut(function() {

        // Change text of the current selection to match the item just clicked (not working)
        $('.filter').find('.current-selection').text($(this).text());   

    }).fadeIn();
});

Upvotes: 0

Views: 26

Answers (1)

Richi Zee
Richi Zee

Reputation: 374

put the text in an extra variable

$('.filter').on( 'click', 'a', function() {
    var value = $(this).html();
    $(this).parent().fadeOut(function() {
        $('.filter .current-selection').text(value);   

    }).fadeIn();
});

Upvotes: 1

Related Questions