Add Function to Existing JQuery Plugin

Is it possible to add a function to a plugin without changing the real plugin? Can I do something like this in my site's js file?

$.fn.Watermark.Refresh = function() { $.Watermark.HideAll(); $.Watermark.ShowAll(); } 

or

 (function($){ $.fn.Watermark.Refresh = function() { $.Watermark.HideAll(); $.Watermark.ShowAll(); }; })(jQuery); 

didn't work, the first one says $ is undefined, the second one says jQuery undefined ...

ideas?

Solution: any of the methods work, just include the jquery file before the site js file.

+4
source share
2 answers

You can add these functions if you want, but you will need to make sure that you also download jQuery itself and the plugin to modify. If you get these errors (that jQuery or "$" is not defined), you did not do it correctly.

Now, although it’s true that you can add these features, I need to wonder what the point is. If I did this, for example:

 $.fn.css.myFunction = function() { return "hello world"; }; 

then you could call it:

 var str = $.fn.css.myFunction(); 

but so what? What is the use of this? I do not think this is very useful.

+2
source

Make sure you enable the plugin after jQuery.

0
source

Source: https://habr.com/ru/post/1335086/


All Articles