Kung Fu Kung Fu

Fujian White Crane Kung Fu

Zope Zope

What I have and am doing with Zope

Photos Photos

Photoalbum, both old and new.

Receptsamlingen Receptsamlingen

In Swedish only. About my "Collection of Recipes" website.

Contact me Contact me

My contact details and how to contact me.

  Mobile version of this page Mobile version of this page


 

setAttribute('style', ...) workaround for IE

createelement, setattribute, style, fontWeight, font-weight

8th of January 2007

I knew had I heard it before but I must have completely missed it anyway and forgotten to test my new Javascript widget in IE. None of the stylesheet worked in IE and it didn't make any sense. Here's how I did it first:

 var closer = document.createElement('a');
 a.setAttribute('style', 'float:left; font-weight:bold');
 a.onclick = function() { ...

That worked in Firefox of course but not in IE. The reason is that apparently IE doesn't support this. This brilliant page says that IE is "incomplete" on setAttribute(). Microsoft sucked again! Let's now focus on the workaround I put in place.

First I created a function to would take "font-weight:bold;..." as input and convert that to "element.style.fontWeight='bold'" etc:

 function rzCC(s){
   // thanks http://www.ruzee.com/blog/2006/07/\
   // retrieving-css-styles-via-javascript/
   for(var exp=/-([a-z])/; 
       exp.test(s); 
       s=s.replace(exp,RegExp.$1.toUpperCase()));
   return s;
 }

 function _setStyle(element, declaration) {
   if (declaration.charAt(declaration.length-1)==';')
     declaration = declaration.slice(0, -1);
   var k, v;
   var splitted = declaration.split(';');
   for (var i=0, len=splitted.length; i<len; i++) {
      k = rzCC(splitted[i].split(':')[0]);
      v = splitted[i].split(':')[1];
      eval("element.style."+k+"='"+v+"'");

   }
 }

I hate having to use eval() but I couldn't think of another way of doing it. Anybody?

Anyhow, now using it is done like this:

 var closer = document.createElement('a');
 //a.setAttribute('style', 'float:left; font-weight:bold');
 _setStyle(a, 'float:left; font-weight:bold');
 a.onclick = function() { ...

and it works in IE!


Comment

ivo van der Wijk - 8th January 2007  [«« Reply to this]
node.style.cssText = "..." usually does the trick for me
Peter Bengtsson - 8th January 2007   [«« Reply to this]
Really? I'll give that a try.
tjallen - 21st November 2007   [«« Reply to this]
Hooray! Thank-you.

In my case, involving styles of a div element, this worked only after I append the div to the body node, but yes!
state-machine - 29th January 2008   [«« Reply to this]
Yes, this is a really short solution! Thank you all for not giving up on this topic. By the way - it's not possible to code a lot of JavaScript for HTML using the DOM only, is it?
Brittany - 9th February 2007  [«« Reply to this]
thanks, it works well!
Ra - 21st May 2007  [«« Reply to this]
BEFORE

var closer = document.createElement('a');
//a.setAttribute('style', 'float:left; font-weight:bold');

_setStyle(a, 'float:left; font-weight:bold');
a.onclick = function() { ... }

----------------------
AFTER

var closer = document.createElement('a');
//closer.setAttribute('style', 'float:left; font-weight:bold');

_setStyle(closer, 'float:left; font-weight:bold');
closer.onclick = function() { ... }

If type 'closer' it's work

Thanks Peter :D
Matt - 8th January 2008  [«« Reply to this]
Just what I was looking for.

Thanks a lot!
agent47 - 3rd May 2008  [«« Reply to this]
Wow that was great buddy ! You saved me a lot of weeks. I need to personally thanks you too.
LP - 14th June 2008  [«« Reply to this]
Hi, it helped me to fix appending of new style values, without overwritting all previously declared ones (just those that are meant to change). Thanks.
If you still don't like eval(), try this:
element.style[k]= v;
You know objects, arrays, objects...
adrian lee - 11th July 2008  [«« Reply to this]
Thanks a lot! perfect code!!!
 
Name:
Email:
hide my email address.

Your email address will be encoded to prevent email-extraction spiders from reading it so you won't get spammed if you decide to show your email address.