0

I'm trying to access the selector of the jQuery object inside of the closure below so that I don't have to specify it and/or cache it. If I replace $(".the_lead") with $this, it won't perform it's action.

Invoking the plugin

$(".the_lead").scroll_lead({speedup: 400});

Block

var $this = $(this);

$(window).scroll(function() {

    var window_height = $(window).height();
    var document_height = $(document).height();
    var hide_lead;
    var scrollTop = $(window).scrollTop();

    console.log($this);

    if(!hide_lead){

        if(scrollTop>(document_height/2)){
            $(".the_lead").slideDown(options.speedup);
            }else{
            $(".the_lead").slideUp(500,function(){
                $(".the_lead").hide();
          });}
         }


        $('#hide_lead').click(function(e){
            //$(".the_lead").parent().parents('div').hide();
            hide_lead = true;           
            e.preventDefault();
        });     

    }); 

Console Output for $(this):

[selector: ".the_lead", context: document, constructor: function, init: function, selector: ""…]
context: #document
length: 0
selector: ".the_lead"
__proto__: Object[0]

1 Answer 1

1

Notice the length: 0 output by the console. The window scroll event fires more or less every pixel the user moves. Without seeing more of your plugin's code I couldn't be sure, but I'd bet $this is correct only the first time the scroll event fires.

Additionally, caching it seems like you'd want to cache only one of the .the_lead elements, no? That's how your code reads. with $this = $(this); but then within the function you seem to want all elements with the class .the_lead. Try directly caching it like this instead. $this = $('.the_lead');

Sign up to request clarification or add additional context in comments.

1 Comment

thank you. i solve the problem i had with stackoverflow.com/questions/5477394/… answer by BGerrissen

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.