tags:

views:

255

answers:

2

Ok, question does not really make sense. Here is the situation. I have legacy code using "Anthem" ajax, and Anthem allows to hook into pre and post callback steps. So if I have code like this, how do I execute code under .each block in cmdSave_PostCallBack. I know I cannot call it the way it is, but what would be a way that would allow this block to be executed when doc 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() {

}
+1  A: 

Create another function (we shall call it donkey), and use that in the each:

(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;
}
Paul
I hope the name of the function was not implying something:). So, jQuery function does not have to be within Query(document).ready(function() {} block.
epitka
:o) That's correct, it can be a named javascript function
Paul
Right, jQuery can be used anywhere.
chaos
A: 

Would it be as simple as ripping the .each out into its own function and calling it both from the ready and the 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;

}
Dan F
Accepted this one since I was able to copy/paste and it worked. Upped the initial answer from IP. Thanks guys.
epitka