Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

element.style

 
Ryan Christiani
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Question.

I know you can style an element like

element.style.backgroundcolor = ...

what are you not able to put the .backgroundcolor into a variable like such...

var bgcolor = "backgroundcolor";
element.style.bgcolor = "red";

I tried it and it doesnt seem to work, I was more or less wonder why not?

Thanks
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65337
97
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because there's no bgcolor property on that object. Just creating a variable in the gobal scope isn't going to magically make such a property appear on an unrelated object.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65337
97
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What would work is something like:
 
Ryan Christiani
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
awesome thank you!

What makes that work exactly? What do the [] signify with style? My thinking is that it is an array cause of the [] ?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65337
97
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It has nothing to do specifically with style. It's the general form of the property reference operator. The . operator is just a short-hand for it when the property name forms a valid identifier.

The [] only indexes into arrays when used with an array.
 
Ryan Christiani
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Awesome, that is very helpful.

Thanks
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic