tags:

views:

43

answers:

2

Dear experts,

Pardon me for giving you a confusing title on this problem. I'm really confused and i don't know how to put it in other words.

So here is what I what to accomplish.

I am making a Custom Javascript Object to store data.

I have automated this process and inserted each instance of the object into an array.

I am making a loop statement to invoke the unique property value of each instance of the object.

When I want to use an expression, the property name of the object would be variable since i don't know which one it is.

Basically I need to incorporate a string value into the prototype expression.

e.g

document.getElementById('text').style."fontsize"=value;

since I cannot do this directly, i thought possibly I could use the eval function:

eval('document.getElementById("text").style.' + buttons[i].cssvalue + '="39px";');

but this still doesn't work.

Can this be fixed or ss there an alternative way to accomplish this?

If there are some unclear stuff, please point out and I will try to elaborate.

Thanks in advance.

+2  A: 

In javascript you can access properties of an object using this notation:

document.getElementById('text').style["fontSize"] = value;

So your code might be:

document.getElementById('text').style[buttons[i].cssvalue] = "39px";
Jakub Hampl
Yeah, or (...).style[property] = value;
Sune Rasmussen
ty sir, but it i can't get it to work at the moment.
Dennis D
i checked everything else and only this works:document.getElementById("text").style.fontSize="30px";
Dennis D
is fontsize be fontSize?
Dennis D
yep it's `fontSize`, S is caps
Anurag
A: 

First thought, look at a Javascript library or framework like JQuery or Dojo they probably have already solved the problem you are looking at. We use JQuery at work but I prefer Dojo's design but it is more targeted at large web applications.

Other than that Jakub's solution should work.

ealgestorm