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.
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.
(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);
and
(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);
And I call it like this:
promise = $.checkMultipleUsers.CheckUsers();
promise.success(function (count) {
}
and
promise2 = $.checkMultipleUsers2.CheckUsers2();
promise2.success(function (count) {
}
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.
This isn’t really a jQuery plugin, this can be done with just a normal function.
Then you can just call it like
CheckUsers($('#myInput').val());