SitePoint Sponsor

User Tag List

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

    dynamically create variable names from a loop

    I am working on a project where I need to redo the same thing with new variable names

    Code JavaScript:
    var paper1 = new Raphael('img1', 500, 500);
    	c1 = paper1.rect(0, 0, 50, 20, 5);
     
     
    	var paper2 = new Raphael('img2', 500, 500);
    	c2 = paper2.rect(0, 0, 50, 20, 5);
    etc.

    I would like to be able to do this with a for loop.

    So in the above example I would want paper+i, img+i, and c+i

    was trying eval("paper"+i) but didn't work.
    thanks

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,684
    Mentioned
    100 Post(s)
    Tagged
    4 Thread(s)
    You can use window['paper' + i];
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript


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
  •