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 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Feb 2010
    Posts
    9
    Thanks
    3
    Thanked 0 Times in 0 Posts

    dynamic (variable) variables (as in php?)

    Hi All,
    I'm thinking surely there is a way to achieve dynamic or variable variables in JavaScript (jQuery)
    much as one might in php where we might have...
    PHP Code:
    <?php
    $params 
    = array("street""city""state","zip_code");
    foreach(
    $params as $key) $$key some_function($key);
    echo 
    $city;    // would output the result of some_function(city);
    ?>
    but I can't see a way to accomplish similar in jQuery (JavaScript)
    where I would like to have something like...
    PHP Code:
    <script>
    params =["street""city""state","zip_code"];
    jQuery.each(params, function() {
        var var 
    this jQuery('#'+this).val();
    });
    alert(city);
    </script> 
    Which (of course) doesn't work, but there must be a way?

  • #2
    Regular Coder Apothem's Avatar
    Join Date
    Mar 2008
    Posts
    380
    Thanks
    36
    Thanked 25 Times in 25 Posts
    I don't know why you would want to do that instead of using objects/associative arrays. But:

    I would think you would use eval() as such:
    Code:
    params =["street", "city", "state","zip_code"]; 
    for(var i = 0; i < params.length(); i++) {
      eval(params[i] + " = null");
    }
    jQuery.each(params, function() { 
        eval(this + " = jQuery('#'+this).val()"); 
    }); 
    alert(city);

    Something like that.

    But I think it would just be more efficient to just do:
    Code:
    params ={"street":null, "city":null, "state":null,"zip_code":null}; 
    jQuery.each(params, function(key, val) { 
        params[key] = jQuery('#'+this).val(); 
    }); 
    alert(params.city); // or params['key']
    *Note: Code is not tested

  • Users who have thanked Apothem for this post:

    WallabyKid (12-30-2011)

  • #3
    New to the CF scene
    Join Date
    Feb 2010
    Posts
    9
    Thanks
    3
    Thanked 0 Times in 0 Posts
    My reason for wanting to do this is purely for the sake of naming consistency when later in the script I want to make an ajax call with a data var like
    PHP Code:
        var data="action=store_address&street="+street+"&city="+city+"&state="+state+"&zip_code="+zip_code
    but I'd settle for
    PHP Code:
            var data="action=store_address&street="+params.street+"&city="+params.city+"&state="+params.state+"&zip_code="+params.zip_code
    given a slightly simplified version of your second suggestion
    PHP Code:
    params =["street""city""state","zip_code"];
    jQuery.each(params, function() {
        
    params[this] = jQuery('#'+this).val();
    });
    alert(params.city); // or params['city']; 
    Thanks for the follow up! I do appreciate it.
    Cheers

  • #4
    Regular Coder Apothem's Avatar
    Join Date
    Mar 2008
    Posts
    380
    Thanks
    36
    Thanked 25 Times in 25 Posts
    The following links can help you accomplish what you want:
    http://api.jquery.com/jQuery.param/
    http://api.jquery.com/serialize/


  •  

    Posting Permissions

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