0

All, I have the following code:

jQuery('#posts-container-infinite').infinitescroll({
        navSelector  : "div.pagination",
                       // selector for the paged navigation (it will be hidden)
        nextSelector : "a.pagination-next",
                       // selector for the NEXT link (to page 2)
        itemSelector : "div.post",
                       // selector for all items you'll retrieve
        errorCallback: function() {
            jQuery('#posts-container').isotope('reLayout');
        },        
        path: function(){
            desturl = '';
            return desturl;
        }
    }

I'm clicking on a link to do some filtering for an isotope. This code is this:

jQuery('.blog-tabs a').click(function(e){
    e.preventDefault();
    var selector = jQuery(this).attr('data-filter');
    $container.isotope({ filter: selector });
});

Is there a way within my click function to set the desturl variable within my function jQuery('#posts-container-infinite').infinitescroll({?

Thanks!

2
  • 3
    Hmm..how about creating a global variable that changes in that click function, and set the desturl to that variable? Commented Nov 21, 2013 at 16:38
  • Agreed. I don't think it's possible, nor should it be, to affect the value of a private variable in another method. Commented Nov 21, 2013 at 16:39

2 Answers 2

1

Try

var desturl =''; //create a variable desturl  here 
jQuery('#posts-container-infinite').infinitescroll({
    navSelector: "div.pagination",
    nextSelector: "a.pagination-next",
    itemSelector: "div.post",
    errorCallback: function () {
        jQuery('#posts-container').isotope('reLayout');
    },
    path: function () {
        return desturl; //use value here
    }
}
jQuery('.blog-tabs a').click(function (e) {
    e.preventDefault();
    var selector = jQuery(this).attr('data-filter');
    $container.isotope({
        filter: selector
    });
    desturl='test'; //assign value here
});
Sign up to request clarification or add additional context in comments.

Comments

1

This is basic variable scoping. Variables that have been declared within a function are only available within that function block (or child functions). Declare the variable on the block level each of your desired functions to use that variable are:

var desturl;

jQuery('#posts-container-infinite').infinitescroll({
        navSelector  : "div.pagination",
                       // selector for the paged navigation (it will be hidden)
        nextSelector : "a.pagination-next",
                       // selector for the NEXT link (to page 2)
        itemSelector : "div.post",
                       // selector for all items you'll retrieve
        errorCallback: function() {
            jQuery('#posts-container').isotope('reLayout');
        },        
        path: function(){
            desturl = '';
            return desturl;
        }
    }

jQuery('.blog-tabs a').click(function(e){
    e.preventDefault();
    var selector = jQuery(this).attr('data-filter');
    $container.isotope({ filter: selector });
});

doing this you can access and change the variable in both functions

Comments

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.