1
var CustomerBoxesLoad = function () {

    var handleAjaxBoxesSelectedCountry = function() {

        $.ajax({
            type: "POST",
            url: 'ajax/selectbox/country_selected.php',
            dataType:'json',
            success: function(data) {

                var select = $("#countryCodeBox"), options = '';
                select.empty();      

                options += "<option value='0'>Select ...</option>";              

                for(var i=0;i<data.length; i++)
                {
                    options += "<option value='"+data[i].id+"'>"+ data[i].name +"</option>";              
                }

                select.append(options);

            }
        }); 

    }

    var handleAjaxBoxesCustomerType = function() {

        $.ajax({
            type: "POST",
            url: 'ajax/selectbox/customer_type.php',
            dataType:'json',
            success: function(data) {

                var select = $("#customerTypeBox"), options = '';
                select.empty();      

                options += "<option value='0'>Select ...</option>";     

                for(var i=0;i<data.length; i++)
                {
                    options += "<option value='"+data[i].id+"'>"+ data[i].name +"</option>";
                }

                select.append(options);

            }
        });     


    }


    return {
        //main function to initiate the module
        init: function () {

        handleAjaxBoxesSelectedCountry();
        handleAjaxBoxesCustomerType();

        }

    };

}();

I have a function calling from a PHP file. At the end of the PHP file i am calling the Ajax function:

    jQuery(document).ready(function() {    
        CustomerBoxesLoad.init();   
    });

All the Ajax select boxes filling works good. Now i want to trigger the Ajax script from a (a href="") tag. But the function will not trigger like this:

a href="javascript:handleAjaxBoxesSelectedCountry();">Refresh Select Box

THIS reference to the JavaScript dont work!!

1
  • Create a wrapper function in global scope that would call your method, and use that one instead. Commented Sep 29, 2014 at 11:41

2 Answers 2

0

add another function to your return:

 return {
    //main function to initiate the module
    init: function () {
        handleAjaxBoxesSelectedCountry();
        handleAjaxBoxesCustomerType();
    }

    handleAjaxBoxesSelectedCountry: function () {
        handleAjaxBoxesSelectedCountry();
    }
};

and the 'a' tag:

<a href="javascript:CustomerBoxesLoad().init();return false;">STH</a>
Sign up to request clarification or add additional context in comments.

3 Comments

I dont want to use the CustomerBoxesLoad() event. I need to call the sub function in this function 'handleAjaxBoxesSelectedCountry()'.
according to your code the "CustomerBoxesLoad" is a function you declared and it returns a class/object containing another function named "init". So to call init function in return of function "CustomerBoxesLoad", we need to first call this function then call init function from the returned object.
Please understand that the CustomerBoxesLoad function works on the window load event. No problem with that. Right now i am searching for a way to reload the function 'handleAjaxBoxesSelectedCountry' function in the javascript file.
0

I solved it myself:

return {

    //main function to initiate the module
    init: function () {

        handleAjaxBoxesCountry();
        handleAjaxBoxesCustomerType();

    },
    init_country: function () {
        handleAjaxBoxesCountry();
    },
    init_type: function () {
        handleAjaxBoxesCustomerType();
    }       

};

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.