1

I have made a jQuery function and want to call it on click event. My code is like below:

<script type="text/javascript">
    (function($){
        $.fn.my_function = function(options){
            return this.each(function(){
                alert("Hello");
            });
        };
    })(jQuery);

    $(document).ready(function(){
        $('#submit_buttom').my_function(); 
    });
</script>

Please help.

Thanks

4 Answers 4

3

Try this:

 (function($){
        $.fn.my_function = function(options){
            var callback = function(){
                alert("Hello");
                //codes goes here
            };
            return this.each(function(){
                $(this).click(callback);
            });
        };
    })(jQuery);

    $(document).ready(function(){
        $('#submit_buttom').my_function(); 
    });
Sign up to request clarification or add additional context in comments.

1 Comment

hi, thanks for the answer and its working fine. I have one more question: How should I call a function inside delegate? Is there anything like: $('body').delegate('#submit_button', my_function());
3
<script type="text/javascript">
    (function($){
        $.fn.my_function = function(options){
            return this.each(function(){
                $(this).click(function(){
                  alert("Hello");
                });
            });
        };
    })(jQuery);

    $(document).ready(function(){
        $('#submit_buttom').my_function(); 
    });
</script>

4 Comments

hi, thanks for the answer and its working fine as well. I have one more question: How should I call a function inside delegate? Is there anything like: $('body').delegate('#submit_button', my_function());
Just declare the function outside like: function Hello() { alert("Hello"); } and the use the function name as the delegate eg.: $("#button").click(Hello); or am I missunderstanding your question ?
Yeah you didn't get my question :). Actually my button is coming dynamically so to add any event or to use if I have to use delegate like $('body').delegate('#submit_button', 'click', function(){}); but here I want to call my_function() when I click that button. Now you get what I want to say?
If you are using latest jquery version (1.7) then you should use On() instead. I'm still not sure I understand what you mean, but if you just want to call a function declared somewhere else it's the same solution as my previous comment. $("body").on("click", "#submit_button", Hello);
2

You can use the .click() event handler:

$('#submit_buttom').click(function(){
    // Can be any function call, but must be within an asynchronous/anonymous function 
    my_function();
});

Comments

0

Try using the .click() function: described here on the Getting started with jQuery tutorial

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.