[Javascript] dynamic variables names

Chris Tifer christ at saeweb.com
Tue Jun 24 13:25:41 CDT 2003


I fully understood what you're doing. I didn't need to see 
the rest. You're trying to dynamically set an image's source.

I'm sure you have them named:

nt_o1, nt_o2, nt_o3, etc...

What I was saying is that you're referencing them incorrectly 
which is why you think you need a dynamic variable.  If you were 
to reference the element through the .image collection, you'd 
be able to access them by name or index. 

Since you're looking to reference them by name (the correct 
choice in my book), you are forming a value, not a variable.  
Your value is the string "nt_o" + the dynamic value of i

So my real suggestion out of that was to reference the image 
through the collection rather than trying to reference it by 
ID.

Chris Tifer
http://emailajoke.com

  ----- Original Message ----- 
  From: Marcelo Simon 
  To: [JavaScript List] 
  Sent: Tuesday, June 24, 2003 1:44 PM
  Subject: Re: [Javascript] dynamic variables names


  Thanks for the answers, but they didn't solve my problem.

  I didn't post the entire function, and that's why I think you say that I don't have a dynamic variable name. ( or maybe I'm wrong ), here's the function

  function note(number){
   for ( var i=1; i < 6; i++){
    if ( i <= number ){
    alert(['nt_0'+i].src); // it's tracing undefined
     ['nt_o'+i].src = '../_img/1_note_0'+i+'_on.gif';
    } else {
     ['nt_o'+i].src = '../_img/1_note_0'+i+'_off.gif';
    }
   }
  }

  what I'm trying to accomplish, is to change some images from 1 to i based on the number I pass to the function (ie. if I pass 3, I want images 1,2 and 3 to be on and 4 and 5 to be off ), so if someone has a different approach to this please share.

  Thanks again
  Marcelo Simon

    ----- Original Message ----- 
    From: Chris Tifer 
    To: [JavaScript List] 
    Sent: Tuesday, June 24, 2003 1:10 PM
    Subject: Re: [Javascript] dynamic variables names


    Do you have a dynamic variable name? I would never use a 
    dynamic variable to be honest with you - or at least haven't ever 
    found the need for one yet, but from the looks of it below, you 
    don't have a dynamic variable, but rather you're trying to build 
    a reference to an image dynamically.

    To do that, use the document.images collection and 
    build your string when targeting a specific element in that 
    collection:

    document.images["nt_o" + i].src = "path/to/image.gif"

    That's not a dynamic variable name - just so you're aware.

    Chris Tifer
    http://emailajoke.com

      ----- Original Message ----- 
      From: Marcelo Simon 
      To: * Yahoo / Javascript 
      Sent: Tuesday, June 24, 2003 11:50 AM
      Subject: [Javascript] dynamic variables names


      Hi list, 

      It's me again, and I need some help again please.

      how can I use dynamic variables name in JS ?

      I mean, how do I evaluate the 2 parts so they work ?

      if ( i <= number ){
         ['nt_o'+i].src = '../_img/1_nota_0'+i+'_on.gif';
      }

      thanks in advance


--------------------------------------------------------------------------


      _______________________________________________
      Javascript mailing list
      Javascript at LaTech.edu
      https://lists.LaTech.edu/mailman/listinfo/javascript



----------------------------------------------------------------------------


    _______________________________________________
    Javascript mailing list
    Javascript at LaTech.edu
    https://lists.LaTech.edu/mailman/listinfo/javascript



------------------------------------------------------------------------------


  _______________________________________________
  Javascript mailing list
  Javascript at LaTech.edu
  https://lists.LaTech.edu/mailman/listinfo/javascript
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.evolt.org/pipermail/javascript/attachments/20030624/7222798b/attachment.htm>


More information about the Javascript mailing list