Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    Senior Coder
    Join Date
    Jun 2008
    Location
    New Jersey
    Posts
    2,535
    Thanks
    45
    Thanked 259 Times in 256 Posts

    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...

  • #2
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,900
    Thanks
    56
    Thanked 542 Times in 539 Posts
    you have no idea whatsoever what the form elements will be? I think we may need a little more detail, if possible

  • #3
    Senior Coder
    Join Date
    Jun 2008
    Location
    New Jersey
    Posts
    2,535
    Thanks
    45
    Thanked 259 Times in 256 Posts
    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.

  • #4
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,237
    Thanks
    12
    Thanked 340 Times in 336 Posts
    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.
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • #5
    Senior Coder
    Join Date
    Jun 2008
    Location
    New Jersey
    Posts
    2,535
    Thanks
    45
    Thanked 259 Times in 256 Posts
    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.

  • #6
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,900
    Thanks
    56
    Thanked 542 Times in 539 Posts
    did you pass the new value to the function, or is that what you're trying to avoid?

  • #7
    Senior Coder
    Join Date
    Jun 2008
    Location
    New Jersey
    Posts
    2,535
    Thanks
    45
    Thanked 259 Times in 256 Posts
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •