Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 10-05-2012, 04:50 AM   PM User | #1
Keleth
Senior Coder

 
Join Date: Jun 2008
Location: New Jersey
Posts: 2,354
Thanks: 45
Thanked 247 Times in 244 Posts
Keleth is on a distinguished road
Passing by reference to function

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?

Javascript is annoying sometimes...
Keleth is offline   Reply With Quote
Old 10-05-2012, 05:02 AM   PM User | #2
xelawho
Senior Coder

 
xelawho's Avatar
 
Join Date: Nov 2010
Posts: 2,437
Thanks: 52
Thanked 454 Times in 452 Posts
xelawho will become famous soon enoughxelawho will become famous soon enough
you have no idea whatsoever what the form elements will be? I think we may need a little more detail, if possible
xelawho is offline   Reply With Quote
Old 10-05-2012, 06:04 AM   PM User | #3
Keleth
Senior Coder

 
Join Date: Jun 2008
Location: New Jersey
Posts: 2,354
Thanks: 45
Thanked 247 Times in 244 Posts
Keleth is on a distinguished road
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.
Keleth is offline   Reply With Quote
Old 10-05-2012, 09:02 AM   PM User | #4
Dormilich
Senior Coder

 
Dormilich's Avatar
 
Join Date: Jan 2010
Location: Behind the Wall
Posts: 2,881
Thanks: 9
Thanked 291 Times in 287 Posts
Dormilich is on a distinguished road
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.
__________________
please post your code wrapped in [CODE] [/CODE] tags
Dormilich is offline   Reply With Quote
Old 10-05-2012, 02:48 PM   PM User | #5
Keleth
Senior Coder

 
Join Date: Jun 2008
Location: New Jersey
Posts: 2,354
Thanks: 45
Thanked 247 Times in 244 Posts
Keleth is on a distinguished road
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.
Keleth is offline   Reply With Quote
Old 10-05-2012, 02:56 PM   PM User | #6
xelawho
Senior Coder

 
xelawho's Avatar
 
Join Date: Nov 2010
Posts: 2,437
Thanks: 52
Thanked 454 Times in 452 Posts
xelawho will become famous soon enoughxelawho will become famous soon enough
did you pass the new value to the function, or is that what you're trying to avoid?
xelawho is offline   Reply With Quote
Old 10-05-2012, 04:58 PM   PM User | #7
Keleth
Senior Coder

 
Join Date: Jun 2008
Location: New Jersey
Posts: 2,354
Thanks: 45
Thanked 247 Times in 244 Posts
Keleth is on a distinguished road
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.
Keleth is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 09:09 PM.


Advertisement
Log in to turn off these ads.