Christian Heilmann

You are currently browsing the Christian Heilmann blog archives for April, 2007.

Archive for April, 2007

Q&A: Dynamically assigning CSS Floating in JavaScript

Monday, April 2nd, 2007

Q: I am creating some HTML elements using JavaScript and the DOM. All is fine, but I need to define one element as floated to the right. I tried var d=document.createElement(‘div’); d.setAttribute(‘float’,’left’); which seems to do it, but the element is not floated. I tried then to use the style collection with d.style.float = ‘left’; but that didn’t work either. How do you float an element in JS?

A: Your first approach was wrong, as this would add an attribute called “float� to the element and not set the CSS property. The second approach is almost right, but “float� is a reserved word in JavaScript (because of floating point numbers). The correct property of the style collection is called cssFloat (you will also run into the same issue when you try to create labels with a “for� attribute, this one is called htmlFor). This’ll work nicely in Firefox, but Internet Explorer does not support cssFloat (one of the annoying bugs that were still not fixed).

In any case, generating a lot of HTML and defining the style inside your JavaScript is not a very maintainable way of development. It is much easier to keep the annoying little browser CSS support glitches to fix in CSS rather than having to check through a script to find where you changed it. In order to allow for that all you need to do is assign either an ID or a class to the parent element of all your DIVs or add a CSS class to the DIV when you create it. Then you can define the floating in CSS and you don’t need to worry about it any longer. Be aware that you need to use the className property (d.className = ‘foo’) to assign a CSS class to an element and not setAttribute(‘class’,’foo’) as class is another reserved word.

Q&A: Internet Explorer keyboard navigation doesn\’t work

Monday, April 2nd, 2007

Q: I have a Table of Contents at the beginning of a page that is linked to anchors around the headlines of all the content sections. When I click them with a mouse there is no problem – I get sent to the heading, read the paragraph and click the “back to top� link to go to the TOC. However, as I need to be accessible with this project I realised that when I use the keyboard and tab from link to link and hit enter I get sent to the section, but hitting tab doesn’t get me to the first link in this section but to the next item in the TOC! What am I doing wrong?

A: You are not doing anything wrong – Internet Explorer is (I assume this is what you are using). Internet Explorer has a really bizarre bug – it scrolls the page to a named anchor but fails to move the keyboard focus.

The fix for the problem has to do with IE’s hasLayout property. This property is set to true whenever an element has a defined height or width, is floated or has some other IE-only properties like zoom set. One solution to make IE move the keyboard focus is to contain the link in an element that has a hasLayout property of true, like a DIV with defined measurements.

This bug has been undiscovered for a long time as table cells by default have a hasLayout of true in IE and therefore the problem never occurred in table based layouts.

Another solution to the problem is to give the link a predefined tabindex property. If you don’t know how many links there are or you need tabindex for other reasons, you can use a tabindex of -1, which is not valid in HTML, but does the trick.

You can read more about this problem and the solutions in more detail over at Gez Lemon’s Juicy Studio :