Dirty Bird Design
Dirty Bird Design

Reputation: 5553

Cannot access function by name inside ajaxComplete?

I have a function that works fine in $(ready):

var externalLink = $("a[href*='/domain.com'], a[href*='.domain.com']");
function externalLinkFix(){
    if((externalLink).hasClass("foo")) {
        $(externalLink).removeClass("foo").addClass("bar").off('click');
    } else {
        $(externalLink).addClass("bar");
    }
}

$(function(){
    externalLinkFix();
});

However, when I call it in the ajaxComplete call it does nothing.

$( document ).ajaxComplete(function(){
    externalLinkFix();
});

I have to include the functions contents inside the ajaxComplete. Why can't I access the function itself by name?

$( document ).ajaxComplete(function(){
    if((externalLink).hasClass("foo")) {
        $(externalLink).removeClass("foo").addClass("bar").off('click');
    } else {
         $(externalLink).addClass("bar");
    }
});

Upvotes: 0

Views: 49

Answers (1)

Ram Segev
Ram Segev

Reputation: 2571

try using ajaxComplete this way you are sure the item is there before you bind it

$( document ).ajaxComplete(function( event, xhr, settings ) {
  if ( settings.url === "ajax/test.html" ) {
    $( ".log" ).text( "Triggered ajaxComplete handler. The result is " +
      xhr.responseText );
  }
});

Upvotes: 1

Related Questions