0

I want to pass a variable into a .load() function, not the URL, but the function that would happen afterwards.

As you can see below, i have an if/else statement. I want to pass a variable into that. At the moment i have 2 variables which sit outside this $next and $prev, these are outside of the method. I have tried making them global but that doesn't work. Ideally i want these to be variables as it is used a couple of times.

var $next = $('.nav.next');
var $prev = $('.nav.prev');


method.open = function(settings) {
    var $link_url = settings.url + ' ' + settings.element;

    $($modal_inner).load( $link_url, function(response, status, xhr ) {
        if (status == 'error') {
        var msg = 'Sorry but there was an error: ';
            $( '#error' ).appendTo( '.modal_inner' );
            $( '#error' ).html( msg + xhr.status + " " + xhr.statusText) ;
        }
        else {
            // Get pagination URL's
            var $pag_next = $(this).find('#post').attr('data-pag-next');
            var $pag_prev = $(this).find('#post').attr('data-pag-prev');

            // If URL's add URL and show 
            if( $pag_next != '') {
                $( $next ).attr('href', $pag_next);
            }
            if( $pag_prev != '') {
                $( $prev ).attr('href', $pag_prev);
            }

        }
    });
};

2 Answers 2

3

You can use variables in your parent scope, either local variables from your parent function or arguments from your parent function. For example, if you passed in next and prev into the open method like this, then you could access them in the load handler function.

method.open = function(settings, next, prev) {
    var $link_url = settings.url + ' ' + settings.element;

    $($modal_inner).load( $link_url, function(response, status, xhr ) {
        // you can access next and prev from your parent scope here
        if (status == 'error') {
        var msg = 'Sorry but there was an error: ';
            $( '#error' ).appendTo( '.modal_inner' );
            $( '#error' ).html( msg + xhr.status + " " + xhr.statusText) ;
        }
        else {
            // Get pagination URL's
            var $pag_next = $(this).find('#post').attr('data-pag-next');
            var $pag_prev = $(this).find('#post').attr('data-pag-prev');

            // If URL's add URL and show 
            if( $pag_next != '') {
                $( $next ).attr('href', $pag_next);
            }
            if( $pag_prev != '') {
                $( $prev ).attr('href', $pag_prev);
            }

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

4 Comments

Thanks @jfriend00, i couldn't quite get this working, if i have the variables set do i then put them in the open method call?
@zizther - I don't understand your question. Any additional data that you want access to in the load handler function can just be passed in as arguments to the open method.
where you have put next and prev into the load handler, is that the variable call or are you referencing them somewhere else? It maybe me not quite understanding it.
@zizther - see how I made them arguments to the open() method definition. You would pass them to your open call: foo.open(settings, $('.nav.next'), $('.nav.prev'));
1

Remove the $() before the $next

$( $next )

Should be

$next

Because $next is already a jQuery Object ..

In the code you are again trying to convert that into a jQuery Object

if( $pag_next != '') {
    $next.attr('href', $pag_next);
}
if( $pag_prev != '') {
    $prev.attr('href', $pag_prev);
}

3 Comments

He could even use next actually
While the extra $() is unnecessary, it works fine so I don't think this is what the OP is asking.
I have tried that, but it still does not seem to reference the element. What it is doing is getting and passing a URL into the element, but it comes back blank at the moment

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.