Storing style properties aside from object

V

vunet

Hello dear experts,
I have my own JavaScript library which includes some visual effects
for HTML elements. Those elements have CSS style properties which I,
for particular reason, do not want to include in CSS file separately
from library. However, I do want to separate styles aside from their
objects in order to keep them all in one place within a library to
easily modify when needed.
My question is how this can be done in a more or less correct way. I
was thinking about something like:

var style1 = {
backgroundColor:"blue",
color:"white"
}

var myDiv = document.createElement("div");
myDiv.style = style1;

The question is how correct that is and/or is there a better way to do
it?
Thanks a lot.
 
T

Thomas 'PointedEars' Lahn

vunet said:
[...] I was thinking about something like:

var style1 = {
backgroundColor:"blue",
color:"white"
}

var myDiv = document.createElement("div");
myDiv.style = style1;

The question is how correct that is

It is not correct at all. The `style' property is specified to be read-only:

and/or is there a better way to do it?

if (myDiv)
{
for (var p in style1)
{
if (typeof myDiv.style[p] != "undefined")
{
myDiv.style[p] = style1[p];
}
}
}

See <http://www.w3.org/TR/DOM-Level-2-Style/css.html#CSS-CSS2Properties>.


PointedEars
 
V

vunet

vunet said:
[...] I was thinking about something like:
var style1 = {
backgroundColor:"blue",
color:"white"
}
var myDiv = document.createElement("div");
myDiv.style = style1;
The question is how correct that is

It is not correct at all.  The `style' property is specified to be read-only:

and/or is there a better way to do it?

  if (myDiv)
  {
    for (var p in style1)
    {
      if (typeof myDiv.style[p] != "undefined")
      {
        myDiv.style[p] = style1[p];
      }
    }
  }

See <http://www.w3.org/TR/DOM-Level-2-Style/css.html#CSS-CSS2Properties>.

PointedEars
--
Anyone who slaps a 'this page is best viewed with Browser X' label on
a Web page appears to be yearning for the bad old days, before the Web,
when you had very little chance of reading a document written on another
computer, another word processor, or another network. -- Tim Berners-Lee

Thank you
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,139
Messages
2,570,805
Members
47,356
Latest member
Tommyhotly

Latest Threads

Top