Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery using (this) in custom function

I created a little jquery script and I have a problem to use (this) in a custom function.

This is the code:

jQuery("li").click(function()
{
    var scrollTop = jQuery(window).scrollTop();
    if(scrollTop > 0)
    {
        jQuery('html, body').animate( { scrollTop: 0 }, 'slow', function()
        {
            fadeItems();
        });

    }
    else
    {
        fadeItems();    
    }

});

function fadeItems()
{       
    var slogan = jQuery(this).children('p').html();

    jQuery('#slogan_text').fadeOut(150, function(){
        jQuery('#slogan_text').fadeIn(150).html(slogan);
    });

    var content = jQuery(this).children('#post_content_large').html();
    jQuery('#content_view').html(content).hide();

    var status = jQuery("#readMore").html();

    if(status == 'Verbergen')
    {
        jQuery('#content_view').fadeIn(500, function(){
            jQuery('#content_view').fadeIn(500).html(content);
        });
    }

    var title = jQuery(this).children('h3').html();

    jQuery('#title_content').fadeOut(150, function(){
        jQuery('#title_content').fadeIn(150).html(title);
    });
}

So the function runs when clicking on a list items and that goes wel but the values of (this) is empty

Somebody know how to fix this?

Thanks in advance!

like image 320
Leon van der Veen Avatar asked Apr 26 '12 14:04

Leon van der Veen


Video Answer


1 Answers

.call can be useful here:

jQuery("li").click(function () {
    var self = this;
    var scrollTop = jQuery(window).scrollTop();
    if(scrollTop > 0) {
        jQuery('html, body').animate( { scrollTop: 0 }, 'slow', function() {
            fadeItems.call(self);
        });    
    }
    else {
        fadeItems.call(self);
    }    
});
like image 72
Kevin B Avatar answered Sep 22 '22 18:09

Kevin B