Sorry for the long question. I have something like the following code:
$("#el1, #el2, #el3, #el4, #el5").on("spinstop", function (event, ui) {
updtpg()
});
#el1
, #el2
etc. are all jQuery UI spinners. There are, however, several more than five on the page (around twenty, but some are sometimes hidden) among other buttons, dropdowns, and checkboxes. I hook up each one to the function updtpg
, which grabs the values from the spinners and does some calculations, updating a results div.
I'm storing jQuery objects for each spinner in a global object like so:
var window.gQ = {};
gQ.$el1 = $("#el1");
gQ.$el2 = $("#el2");
... and using these in the updtpg
function to grab the values of the spinners. What I'd like to do is also use properties in this global gQ object to hold the values of these spinners. That means, however, hooking up each spinner like so:
gQ.$el1.on("spinstop", function (event, ui) {
gQ.$val1 = gQ.$el1.val();
updtpg();
});
gQ.$el2.on("spinstop", function (event, ui) {
gQ.$val2 = gQ.$el2.val();
updtpg();
});
...and so on for every spinner. I don't mind doing that if it makes sense. And I guess that's my question. Is that the "correct" way. I think so because I don't want to have to read the val() function for each spinner every time one of them changes, but it also adds a lot of code. Pretty repetitive code, to boot. If there's a better way, and I can't imagine there's not, what is it?
And feel free to correct anything else you see that looks... uh, improvable. Thanks in advance.