Connecting Tech Pros Worldwide Forums | Help | Site Map

Re: Storing style properties aside from object

Thomas 'PointedEars' Lahn
Guest
 
Posts: n/a
#1: Sep 10 '08
vunet wrote:
Quote:
[...] 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:

<http://www.w3.org/TR/DOM-Level-2-Style/css.html#CSS-ElementCSSInlineStyle>
Quote:
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

vunet
Guest
 
Posts: n/a
#2: Sep 10 '08

re: Re: Storing style properties aside from object


On Sep 10, 1:53*pm, Thomas 'PointedEars' Lahn <PointedE...@web.de>
wrote:
Quote:
vunet wrote:
Quote:
[...] I was thinking about something like:
>
Quote:
var style1 = {
backgroundColor:"blue",
color:"white"
}
>
Quote:
var myDiv = document.createElement("div");
myDiv.style = style1;
>
Quote:
The question is how correct that is
>
It is not correct at all. *The `style' property is specified to be read-only:
>
<http://www.w3.org/TR/DOM-Level-2-Style/css.html#CSS-ElementCSSInlineS...>
>
Quote:
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
Closed Thread