Forum Moderators: open

Message Too Old, No Replies

How to pass a reference to a property

         

DyeA

6:35 pm on Mar 12, 2007 (gmt 0)

10+ Year Member



Hello all,

This is my first post, I have lurked for a while and gleaned some great stuff from this site, thanks very much for that. I have worked with Javascript for a while but not having a formal education is tough. I feel like I'm missing some key concepts and it often results in me starting some code with an idea of what I want to happen and an idea of how to implement it but then hit a roadblock and have to switch to an alternate method of operation. Some things are hard to google for an answer as well because there is no easy phrasing for what your looking for. Sorry for the long intro.

My current issue is one i have faced before and haven't been able to google succesfully.

Example
var sty = 'color';
att = 'red';
document.getElementById('myId').style.sty = att;

Its obvious, I'm sure, to you guys, that I am going about this wrong as it does not work! Trying to pass the style 'color' as a variable into this line of code does nothing.

Any enlightenment is appreciated!

Ward

ericjust

6:54 pm on Mar 12, 2007 (gmt 0)

10+ Year Member



Try:

document.getElementById('myId').style[sty] = att;

DyeA

7:04 pm on Mar 12, 2007 (gmt 0)

10+ Year Member



Wow thanks Eric. That worked perfectly, so instead of using the dot syntax style I can use an array type of notation apparently and that works using a variable. Thanks again!