...

View Full Version : newbie. Variable names contained in variables



Roy Gardiner
07-16-2004, 12:46 PM
Can an attribute be made variable? For example, could
document.getElementById('body').style.backgroundColor = '#ffffff' become
document.getElementById('body').style.--something or other-- = --some expression-- where --something or other -- might be a variable containing or a function returning the name of the attribute to be set?

trib4lmaniac
07-16-2004, 12:53 PM
document.getElementById('body').style[attribute]=watever;

Roy Gardiner
07-16-2004, 01:03 PM
document.getElementById('body').style[attribute]=watever;OK thank you, but I think I chose a poor example. Isn't it that the particular feature I chose as an example -- style -- is an array, and can be done that way?

I'm looking at a general way to hold variables within variables; I'm guessing that there is no such feature. Well I can't find one anyway.

trib4lmaniac
07-16-2004, 01:07 PM
I'm not entirely sure what you mean then! That method can be used very well, in many different ways.

document.body.style.background='#369';
document.body.style['background']='#369';
document.body['style']['background']='#369';
document['body']['style']['background']='#369';

glenngv
07-16-2004, 01:13 PM
trib4lmaniac is correct. Consider this demo:


function setStyle(id, attName, attValue){
document.getElementById(id).style[attName]=attValue;
}
...
<div id="myDiv">Blah Blah</div>
<input type="button" value="Change Background" onclick="setStyle('myDiv','backgroundColor','yellow');" />
<input type="button" value="Change Color" onclick="setStyle('myDiv','color','red');" />

More info on bracket notation here (http://www.litotes.demon.co.uk/js_info/sq_brks2.html).

Kor
07-16-2004, 01:24 PM
First, what do you want exactly? To get a styled attribute or to set a styled attribute? Or to get a class? To change a class?

Roy Gardiner
07-16-2004, 01:27 PM
Hmm, I think I'm perhaps asking the wrong question and that the feature you outline is in fact what I want. Thank you.

What I was asking was a subset of 'can you dynamically build code (into a variable) and then execute it?'

trib4lmaniac
07-16-2004, 01:31 PM
Well - for more general code/strings - there's always eval(), but I don't think many people here would recommend that!

Roy Gardiner
07-16-2004, 02:15 PM
First, what do you want exactly? To get a styled attribute or to set a styled attribute? Or to get a class? To change a class? 1) I must choose my examples better :).
2) In the particular case I am trying, to set attributes. I think the [] notation is what I want
Well - for more general code/strings - there's always eval(), but I don't think many people here would recommend that! Because of the inefficiencies? And/or because it's hard to understand?

glenngv
07-19-2004, 03:00 AM
Because of the inefficiencies? And/or because it's hard to understand?
http://www.codingforums.com/showthread.php?t=20143



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum