aspose file tools*
The moose likes HTML, CSS and JavaScript and the fly likes element.style Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "element.style" Watch "element.style" New topic
Author

element.style

Ryan Christiani
Ranch Hand

Joined: May 15, 2011
Posts: 30
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


Probably going to ask a lot of possibly stupid questions...but how else will I learn? am I right!
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60975
    
  65

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.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60975
    
  65

What would work is something like:
Ryan Christiani
Ranch Hand

Joined: May 15, 2011
Posts: 30
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

Joined: Jan 10, 2002
Posts: 60975
    
  65

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

Joined: May 15, 2011
Posts: 30
Awesome, that is very helpful.

Thanks
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: element.style