How to call JQuery from outside of a JQuery block

Okay, the question doesn't make sense. Here's the situation. I have legacy code using "Anthem" ajax and Anthem allows you to hook into pre and post callback stages. So if I have code like this, how do I execute the code in the .each block in cmdSave_PostCallBack. I know I cannot call it the way it was, but how would this block be executed when the document is loaded and also in the postcallback function?

 jQuery(document).ready(function() {
    //select all anchors in divMain element and attach click handler
    $("#<%=divSelectorLinks.ClientID %> a").click(function(e) {
        e.preventDefault();
        var content = $(this.getAttribute("rel"));
        //attach nyromodal
        $.nyroModalManual({
            bgColor: '#efefef',
            width: 200,
            content: content
        })
    })
    //display currently selected value
    .each(function(i) {
        var selectionText = $(this.getAttribute("rel")).find(':selected')[0].innerHTML;
        if (selectionText.substr(0, 4) == "All ") {
            this.innerHTML += ": All";
        }
        else {
            this.innerHTML = this.innerHTML + ": " + selectionText;
        }
    });
    return false;
});

function cmdSave_PostCallBack() {

}

      

+2


source to share


2 answers


Would it be as simple as cutting .each

into your own function and calling it from both pre-made and from cmdSave_PostCallBack

?

Something like:



jQuery(document).ready(function() {
    //select all anchors in divMain element and attach click handler
    $("#<%=divSelectorLinks.ClientID %> a").click(function(e) {
        e.preventDefault();
        var content = $(this.getAttribute("rel"));
        //attach nyromodal
        $.nyroModalManual({
            bgColor: '#efefef',
            width: 200,
            content: content
        })
    });
    displayCurrentlySelectedValues();
    return false;
});

function cmdSave_PostCallBack() {
    displayCurrentlySelectedValues();
}

function displayCurrentlySelectedValues() {
    //Note how this uses the same selector as in .ready
    $("#<%=divSelectorLinks.ClientID %> a")
    .each(function(i) {
        var selectionText = $(this.getAttribute("rel")).find(':selected')[0].innerHTML;
        if (selectionText.substr(0, 4) == "All ") {
            this.innerHTML += ": All";
        }
        else {
            this.innerHTML = this.innerHTML + ": " + selectionText;
        }
    });
    return false;

}

      

0


source


Create another function (we'll call it donkey) and use it in each one:

(sorry, the formatting is a little messed up)



...
.each(donkey);

function cmdSave_PostCallBack() {
    donkey(1);
}

function donkey(i) {
    var selectionText = $(this.getAttribute("rel")).find(':selected')[0].innerHTML;
    if (selectionText.substr(0, 4) == "All ") {
        this.innerHTML += ": All";
    }
    else {
        this.innerHTML = this.innerHTML + ": " + selectionText;
    }
    });
return false;
}

      

+1


source







All Articles