[Javascript] dynamic variables names

Peter Brunone peter at brunone.com
Tue Jun 24 14:54:00 CDT 2003


   FWIW, when Marcelo responded to your posts, I was surprised since I never got them.

---------- Original Message ----------------------------------
From: "Chris Tifer" <christ at saeweb.com>

>Use the images collection. Avoid eval whenever possible...
>
>Did my posts not make it to the list?
>
>
>----- Original Message -----
>From: "Marcelo Simon" <grillo at lazzuri.com.br>
>To: "[JavaScript List]" <javascript at LaTech.edu>
>Sent: Tuesday, June 24, 2003 2:55 PM
>Subject: Re: [Javascript] dynamic variables names
>
>
>> Almost !!!
>>
>> using eval(); i got the first part correct, now alert(eval("nt_0"+i).src)
>> traces to the correct image, but now when I mouse over, the browser is
>> showing me an syntax error that I can't find.
>>
>> sorry to bother again, but does anyone has a clue, on how to fix it ?
>>
>> function nota(numero){
>>  for ( var i=1; i < 6; i++){
>>   if ( i <= numero ){
>>    eval("nt_0"+i).src = eval("../_img/1_nota_0"+i+"_on.gif");
>>   } else {
>>    eval("nt_0"+i).src = eval("../_img/1_nota_0"+i+"_on.gif");
>>   }
>>  }
>> }
>>
>> Thanks again to everyone for their patience :)
>> Marcelo Simon
>>
>> ----- Original Message -----
>> From: "Peter Brunone" <peter at brunone.com>
>> To: "[JavaScript List]" <javascript at LaTech.edu>
>> Sent: Tuesday, June 24, 2003 3:38 PM
>> Subject: Re: [Javascript] dynamic variables names
>>
>>
>> > Marcelo,
>> >
>> >    I'm not sure if this will completely answer your question, but you
>can
>> create Javascript code from a string using the eval() function... so if
>you
>> want to refer to an image called 'myGif', you can set a variable equal to
>> eval('myGif') and then call that variable's .src property.  Of course, you
>> can also use the available collections and getElementByID as Chris has
>shown
>> below.
>> >
>> > Regards,
>> >
>> > Peter
>> >
>> > ---------- Original Message ----------------------------------
>> > From: "Marcelo Simon" <grillo at lazzuri.com.br>
>> > Reply-To: "[JavaScript List]" <javascript at LaTech.edu>
>> > Date:  Tue, 24 Jun 2003 14:44:25 -0300
>> >
>> > >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 
             


More information about the Javascript mailing list