0

I have a situation where I want to call a function from document ready and on click.

var load_report = function(params) {
    report_name = params.data.report_name;
    // code continues
}

I can call it on click -

$("#report-" + report_name).click({'report_name': report_name}, load_report);

However, I also want to call if from $(document).ready() if certain constraints are met -

    $(document).ready(function() {
        if (($.url().param()) && ($.url().param('report_name'))) {
            report_name = $.url().param('report_name');
            // Call load_report with report_name parameter
        }
    }

I have tried - load_report({'report_name': report_name}), $.call({'report_name': report_name}, load_report), $({'report_name': report_name}, load_report); and none of them work.

load_report({'report_name': report_name}) looked the simplest and most promising, but fails on params.data.report_name because params.data is undefined. Basically, all I need to do here is convert the dictionary into a format that can be interpreted the same way as it is with the .click() event data.

My question is - how do I call load_report function with my parameters without binding it to an event?

2 Answers 2

2

You'll have to remember the data key

load_report({data : {'report_name': report_name}});

or just trigger the event

$("#report-" + report_name).trigger('click');
Sign up to request clarification or add additional context in comments.

Comments

0

All you would have to do is load_report({'report_name': this.id.replace('report-', '')});.

$("#report-" + report_name).click(function(){
    load_report({'report_name': this.id.replace('report-', '')});
});

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.