1

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

1
  • 1
    Try calling your function like this: $().Update();. Commented Jul 27, 2014 at 11:50

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Accepted as an answer as I think it is correct when adding an event/manipulating a DOM, It is not applicable to my situation. for anyone looking to use plugins inside the click of a button then $().Update did the trick for me.
@JackM If your plugin doesn't interact with DOM, you can simply attach it to $: $.Update = function () {...}. Then you will call it directly: $.Update().
0

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/

1 Comment

Sorry, my bad. I forgot that it was using $.fn @Ben

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.