Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to pass variables to a jQuery Plugin
    text
    copied!<p>I have a jQuery plugin that someone helped me write. I originally only needed it for one use so hardcoded the textbox value lookup directly in the plugin, but now I would like to pass that variable in from where I call it so I can reuse the same plugin multiple times.</p> <p>Here is what I have, I will post both plugins so you will see what I have done to get it to work side by side. I would like to pass in the UserSearch: variable instead of hard coding it.</p> <pre><code>(function ($) { $.checkMultipleUsers = (function (my) { my.CheckUsers = function () { return $.ajax({ type: "POST", url: "http://localhost:52350/FabRouting/Webservice/UserList.asmx/GetUserCount", data: JSON.stringify({ UserSearch: $("[id$=txtSubmittedBy]").val() }), contentType: "application/json; charset=utf-8", dataType: "json", }); }; return my; })({}); })(jQuery); </code></pre> <p>and</p> <pre><code>(function ($) { $.checkMultipleUsers2 = (function (my) { my.CheckUsers2 = function () { return $.ajax({ type: "POST", url: "http://localhost:52350/FabRouting/Webservice/UserList.asmx/GetUserCount", data: JSON.stringify({ UserSearch: $("[id$=txtOther]").val() }), contentType: "application/json; charset=utf-8", dataType: "json", }); }; return my; })({}); })(jQuery); </code></pre> <p>And I call it like this:</p> <pre><code>promise = $.checkMultipleUsers.CheckUsers(); promise.success(function (count) { } </code></pre> <p>and</p> <pre><code>promise2 = $.checkMultipleUsers2.CheckUsers2(); promise2.success(function (count) { } </code></pre> <p>Also, while I have posted this plugin, is it written well? I don't understand why it needs both the checkMultipleUsers and the CheckUsers. It seems like it is a function embedded in another function and I don't understand why.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload