10-05-2012, 05:50 AM
So I'm making a jQuery function to which I'd like to pass a JSON containing two data from two form elements. I'd like to have the function take the current data in the fields when it works. However, as I don't know what the form elements may be, I can't hard code the selection to the function.
So how can I pass the data by reference? Or how can I pass the appropriate selector data along, in a key/value pair, so I can create the appropriate JSON for the AJAX in the function?
10-05-2012, 06:02 AM
you have no idea whatsoever what the form elements will be? I think we may need a little more detail, if possible
10-05-2012, 07:04 AM
What I'm trying to do is make an autocomplete for one of my sites. Autocomplete, on its own, is pretty straight forward, and I got it working with some nice fun features (up/down arrows to move between selections and what not).
It got me thinking, what if I wanted the autocomplete to select between one list if a checkbox wasn't checked, and a different set if it was? How about if I wanted a radio box to alter the selection, or use a second input to filter the results?
So I thought about how I could pass these values into the autocomplete without necessarily knowing what these components would be. I may want to do an autocomplete by game (I'm building an online pen and paper gaming site), so there could be a series of checkboxes to select game types. Or maybe make a system where you can put friends into groups, then search only within that group. There are a lot of possibilities.
Of course, I don't want to repeat the code over and over, with only slight modifications as to the key for a particular value, and the selector to get that value, so I'm trying to figure out how I can pass the data along. I did consider passing the key and the selector, which would allow me to then just run the selector in the function, but I feel like this would be inefficient (I'd need to do a bunch of checks for checkboxes and radio boxes), so I'm hoping to create a JSON and pass it by reference, at this seems to make the most sense. If its not possible, I'll fall back on passing key/selector pairs and then doing the selector within.
I know its not a concrete example of what I want to do, but this is as much a learning experience as anything else... I feel like knowing how to pass information with limited forehand knowledge would be useful.
10-05-2012, 10:02 AM
straight answer of the thread title: anything that is an object (i.e. anything that is not: undefined, null, a string/boolean/number literal) is passed by reference. that includes: HTML elements, Arrays, JSON objects, object literals, Dates, Functions and many more.
10-05-2012, 03:48 PM
I had read the same, but I tried that to no avail, lest I did something wrong.
I created a JSON before the call to the function, passed it to the jQuery function, wrote it to the console when someone focused on the input in question. Then I wrote a second function that on a timeout changed the JSON value (also confirming with a console log), and tried the focus again, and the value hadn't changed.
10-05-2012, 03:56 PM
did you pass the new value to the function, or is that what you're trying to avoid?
10-05-2012, 05:58 PM
Well, as its a jQuery function, a prototype, what I want to do is tell it to act on a certain element, then be self sustaining. As it does an AJAX call based on the data it collects, I want to tell it what other data to append, but I'd like that data to be dynamic rather then static. I'm not even sure how I could pass it to the function after change.