/    Sign up×
Community /Pin to ProfileBookmark

Trouble styling dynamically created elements in IE

Hello,

I am using javascript to dynamically create a <p> within my HTML page. I have a separate style sheet setup to control the presentation of this element. This works fine in FireFox and Safari, but IE seems to ignore the styling. Is this an IE bug or am I doing something wrong? Is there a workaround?

Here is my code, triggered by a ‘change’ event on a <select> tag.

[CODE]var message = document.createElement(“p”);
message.setAttribute(“class”, “message”);
var theParent = this.parentNode;
var nextNode = this.nextSibling;
theParent.insertBefore(message, nextNode);[/CODE]

[CODE].message {
font-size: 0.8em;
color: #666;
}[/CODE]

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@KravvitzAug 24.2007 — IE is stupid. You would need to add this line for IE:
setAttribute('className', "message")
I don't use getAttribute() or setAttribute() because of IE's stupidity and because using the old DOM0 way works and requires a little less code. So, I recommend you just use this:
className = "message";
Copy linkTweet thisAlerts:
@phadedauthorAug 24.2007 — Thanks Kravvitz. I actually JUST came across an old post of yours addressing this same issue. I made the change. Thanks again! Confounded IE!
×

Success!

Help @phaded spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...