By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,695 Members | 1,601 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,695 IT Pros & Developers. It's quick & easy.

Re: Storing style properties aside from object

P: n/a
vunet wrote:
[...] 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>
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
Sep 10 '08 #1
Share this Question
Share on Google+
1 Reply


P: n/a
On Sep 10, 1:53*pm, Thomas 'PointedEars' Lahn <PointedE...@web.de>
wrote:
vunet wrote:
[...] 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-ElementCSSInlineS...>
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
Sep 10 '08 #2

This discussion thread is closed

Replies have been disabled for this discussion.