Jack M
Jack M

Reputation: 2574

jquery plugin and a Click button

I think this must be easy but I am failing to make it work. I Have a this from my plugin

(function($) {
$.fn.Update = function() {
   console.log("Clicked");
}; 
})(jQuery);

and this on the page where I want to use:

<script src="~/Scripts/my.Plugin.js">

</script>

$(function() {
    $(".btnUpdate").click(function() {
        //TODO: Add the function from the plugin
        Update();
    });        
});

but I am getting ReferenceError: update is not defined and noticed that when the page load, I don't go inside the function, I only touch on the $.fn.update and move straight to the end.

Any help welcome

Upvotes: 1

Views: 69

Answers (2)

Kyle Emmanuel
Kyle Emmanuel

Reputation: 2221

You're calling a method from your own plugin.

You can either bind it to an element like $("#some_element").Update();

Read more here: http://learn.jquery.com/plugins/basic-plugin-creation/

Upvotes: 0

Ionică Bizău
Ionică Bizău

Reputation: 113385

Using $.fn.Update = function() {...} you add a new method to jQuery selected elements.

You can call that function using:

$.fn.Update();

or

$("...").Update();

Using $(foo).Update(), $(foo) will be this inside of the Update function.


You get Update is not defined error because you don't have any Update function (that is declared globally), but $.fn.Update.

Upvotes: 3

Related Questions