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

IE: apply stylesheet in dynamically inserted nodes

P: n/a
I'm writing a small script to scan an HTML document and append a <span
class="foo">Bar</span> node after each item with certain class name.

My script works fine in Gecko but IE has a serious issue: the CSS style
defined in an external sheet for span.foo is not applied. The generated
HTML shows the correct attribute has been inserted (I can see it with an
old "View Partial Source" hack I've kept for years). Thank you for any
hint.

Here's the relevant code:
// obj is an array with nodes

for(var i=0; i<obj.length; i++){
// Create the <span>
var span=document.createElement('span');
span.setAttribute('class', 'foo';

// Creamos <span>'s content
var txt=document.createTextNode('Bar');

// Compose <span>
span.appendChild(txt);

// Insertamos <span> in document
if(obj[i].nextSibling){
obj[i].parentNode.insertBefore(span, obj[i].nextSibling);
}else{
obj[i].parentNode.appendChild(span);
}
}

--
-- Álvaro G. Vicario - Burgos, Spain
-- http://bits.demogracia.com - Mi sitio sobre programación web
-- Don't e-mail me your questions, post them to the group
--
Aug 30 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a


Alvaro G Vicario wrote:

var span=document.createElement('span');
span.setAttribute('class', 'foo';


Don't use setAttribute, use
span.className = 'foo';
instead and your cross browser problems go away.

--

Martin Honnen
http://JavaScript.FAQTs.com/
Aug 30 '05 #2

P: n/a

As Martin said, IE does have issues with .setAttribute().
Danny
Aug 31 '05 #3

P: n/a
*** Martin Honnen wrote/escribió (Tue, 30 Aug 2005 15:48:28 +0200):
var span=document.createElement('span');
span.setAttribute('class', 'foo';


Don't use setAttribute, use
span.className = 'foo';
instead and your cross browser problems go away.


Oh my, the solution is always soooo evident after you've been told! Thanks
a lot.
--
-- Álvaro G. Vicario - Burgos, Spain
-- http://bits.demogracia.com - Mi sitio sobre programación web
-- Don't e-mail me your questions, post them to the group
--
Aug 31 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.