javascript - Calling JQuery function from xhtml in external js file -


I have a jquery function defined in an external JS file: myJquery.js

  (Function ($) {$ .fn.test = function (options) {// Extended default option var opts = $ .extend ({chars: 0}, options); ... doSomething ...} (jQuery) ;  

Including this file as XHTML:

    

And try to use it as:

  $ (document) .ready (function () {$ ("input.testClass"). Each (function () {$ (this). Test ({character: 10});});});  

But there is a JS error, not telling the 'test' function.

Your test function is scotched to the external file location, not Global environment. If you are sure that there is an order of $ in jQuery, then function ($) attach your function without the cover.

EDIT: The above is still relevant to others ... it works with } is ok (at least showing only the warning):

External Code & lt; Script type = "text / javascript" src = "test.js"> gt; & Lt; / Script & gt;

  (function ($) {$ .fn.test = function (option) {// Extended default option var opts = $ .extend ({chars: 0}, Options ); Alert ('boo:' + opts.chars);}}) (jQuery);  

In the page:

  & lt; Script type = "text / javascript" & gt; $ (Document) .ready (function () {$ ("h1"). Each (function () {$ (this) .test ({chars: 10});});}); & Lt; / Script & gt;  

Comments