1

I have been working on a jQuery-like JavaScript library. The next function I would like to add is a .css() function.

Something like this:

function css(elem,name,value){
   elem.style.name = value;
   //   >>>>   ^   <<<<<<
   // needed variable variable
}

Anyone know what I should do? I am at a loss, I have been Goggling suggestions for the past hour and I haven't come up a solution.

Any ideas?

Thanks.

Progo
  • 3,452
  • 5
  • 27
  • 44
  • @Pointy Oh, I hadn't seen that. If I had, I wouldn't have asked this question. But as I said, I have been Goggling for the past hour. – Progo Nov 07 '13 at 23:01
  • You've googled this for an hour and couldn't find the answer? That's hard to believe. I searched "get object property using variable", and the first result (and many more) showed how. – Blue Skies Nov 07 '13 at 23:01
  • @BlueSkies Well, I hadn't searched that specifically. I had been searching things along the lines of `JavaScript variable variables`. – Progo Nov 07 '13 at 23:04
  • Searching "JavaScript variable variables" gives the solution as well. – Blue Skies Nov 07 '13 at 23:14

1 Answers1

5

You're looking for bracket notation:

function css(elem,name,value){
    elem.style[name] = value;
}
Jason P
  • 26,984
  • 3
  • 31
  • 45