views:

55

answers:

2

Hi,

I have this stylesheet:

.pixel{
    position: absolute;
    height: 10px;
    width: 10px;
    background-color: #ffcc00;
    font-size: 0px;
}

And this javascript:

function drawpixel(x,y){
    el = document.createElement('div');
    el.setAttribute('class','pixel');
    el.style.left = x;
    el.style.top = y;
    /*  IE needs this for some reason?
    el.style.position = "absolute";
    el.style.backgroundColor = "#FFCC00";
    el.style.width = "2px";
    el.style.height = "2px";
    */
    el.style.fontSize = "0px";
    el.style.lineHeight = "0px";
    document.body.appendChild(el);
}

function mover(event){
    element = document.getElementById("coords");
    element.innerHTML = event.clientX + " " + event.clientY;
    drawpixel(event.clientX, event.clientY);
} 

Which allows me to "draw" with divs. But IE requires those 4 commented lines - it won't read the class information for some reason?

Is there any easier way to "fix" it for IE, or do I pretty much just have to have those 4 lines?

thanks

+4  A: 

In IE (due to this bug) you need to change this:

el.setAttribute('class','pixel');  

to

el.setAttribute('className','pixel');  

IE's implementation of setAttribute() is woefully broken before IE8 (running in IE8 standards mode)

scunliffe
You should never try to set a class name using `setAttribute()`. Just use `el.className` instead.
Marcel Korpel
using `setAttribute()` is absolutely fine, as long as you use `className`. I'm not going to avoid using standard ECMAScript DOM manipulation methods just because old legacy versions of IE couldn't implement the specs correctly (end rant)
scunliffe
IE's implementation of *everything* is woefully broken, I've come to discover. el.className works back to IE 5, though, which *should* be the furthest back I'll have to support. This is all intranet sites, and right now I'm just learning the web front-end stuff, and learning new and exciting reasons to hate Internet Explorer.
Wayne Werner
`className` goes back to IE 4. Also, `className` works uniformly across all the major scriptable browsers since IE 4, it's not going away so standard or no standard, it is the best thing to use.
Tim Down
I regretfully currently support IE6 on existing projects - but new projects do not support IE6 at all. As for IE5.5, IE5, and IE4 - they are beyond dead browsers - I refuse to support them.
scunliffe
scunliffe: That's fine, I'm not suggesting you should support IE 4, 5 or 5.5. I'd be very surprised if any specification says anything to suggest `el.setAttribute('className','pixel');` should mean setting the `class` attribute, whereas the `className` property does appear in the W3C DOM 2 HTML spec: http://www.w3.org/TR/DOM-Level-2-HTML/html.html#ID-95362176
Tim Down
I agree, both methods work and either is fine. I think I got on the defensive after @Marcel's comment of *"You should never try to set a class name using setAttribute()"* which rubbed me the wrong way. In reality due to IE's lack of standards I'd actually advocate using jQuery or similar to abstract away IE's mountain of bugs. ;-)
scunliffe
+5  A: 

Just use el.className = "pixel";

Just something else I noticed. I know the question states that this is for IE but it looks like you are using your mover(event) in an event handler. If you are using Firefox and other browsers you may want to consider coding the mover function like this:

function mover(evt){  
    evt = evt || event;                                                           
    element = document.getElementById("coords");                                 
    element.innerHTML = evt .clientX + " " + evt .clientY;                     
    drawpixel(evt .clientX, evt .clientY);                                     
} 
John Hartsock
Well that's good to know. I had tried the el.className before but I guess something else wasn't working. For the event thing, the tutorials I've read actually used event that way. Thanks for the correction!
Wayne Werner
Your `event.clientX` and `event.clientY` should be `evt.clientX` and `evt.clientY`.
Tim Down
good catch...thanks, I corrected the code
John Hartsock