SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Member
    Join Date
    Jul 2010
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    dynamic variable name

    Hello,

    I would like to create dynamic variable name and assign values to them.
    Something like this:

    var item1 = "adf";
    var item2 = "wer";
    :
    var item'n' = "xyz";

    i want to create these names dynamically.

    i tried: var item{i} where i is the dynamic variable.
    var item[i]
    var item(i)
    nothing seems to be working.

    I also tried:
    eval('item'+i) - it gives 'item1' is undefined.
    It doesnt recognize : var eval('item'+i)

    Any idea ??

    Thanks
    J

  2. #2
    SitePoint Wizard bronze trophy chris.upjohn's Avatar
    Join Date
    Apr 2010
    Location
    Melbourne, AU
    Posts
    2,197
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    An array is what you want to be using for dynamic variables. See my example below

    Code JavaScript:
    var item = new Array();
    item['test'] = 'Hello';
    item['test2'] = 'Hello Again';
     
    alert(item['test']);

    Hope that helps bud


Bookmarks

Posting Permissions

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