Do not use the style property on HTML objects

Since ajax, a lot of people have to create HTML on the fly depending on response of the request. One mistake that I see a lot is that people are using the style property of their HTML objects. Why shouldn’t I do it?

The style property has a precedence on every style

Because the style property is so “close” to the HTML object, it will override every other style from a CSS file. So if you begin to write styles in your javascript code, you’ll have to do it all the time. It might be easy and clean for a basic project but it will become a mess in the future. You should centralize every style in your CSS files so you won’t have to search where you assigned a style.

Example

I have this CSS

[source:css]
div.theCssClass {
background-color:blue;
}
[/source]

and I create a new HTML object of this class

[source:javascript]
// I create the DIV
var div = document.createElement(“div”);
div.appendChild(document.createTextNode(“This div is a test”));

div.className = “theCssClass”; // I assign theCssClass
div.style.backgroundColor = “red”; // The background is red

document.body.appendChild(div);
[/source]

You’ll see that the even if I set the background color to blue in the CSS file, the background will be red because I used the style property.

The !important in CSS

If you want to override a style assigned in javascript, you could always use the !important statement in your CSS file.

[source:css]
div.useThisOne {
background-color:blue !important;
}
[/source]

Use it only if you really have to. I wouldn’t recommend !important because it breaks the default behavior of styles and can become very messy. When all is important, nothing is important.

  • I haven't had a chance to write it up yet, but there is a handy little function called 'cssjs' that I think 'Christian Heilmann' (icant.co.uk) wrote:

    --------------------------

    function cssjs(action, object, className) {

    if (action == 'add') {

    if (!cssjs('check', object, className)) {
    object.className += (object.className == '' ? '' : ' ') + className;
    }

    } else if (action == 'remove') {

    var exp = new RegExp('(^' + className + '( |$)| ' + className + '\\b)');
    object.className = object.className.replace(exp, '');

    } else if (action == 'check') {

    var exp = new RegExp('\\b' + className + '\\b');
    return exp.test(object.className);

    }

    return true;

    }

    --------------------------

    http://www.onlinetools.org/articles/unobtrusive...

    NOTE: I have slightly edited the code as I did not need the 'swap' feature, and also it needed some cleanup when it came to running in Firefox with Strict JavaScript error reporting enabled.

    Anyway, it provides a simple interface for adding, removing or checking for the presence of class names.

    This gets around the problem of setting the '.className' and over-riding the class values from other scripts running on the website.

    As another little tip... try to prefix your class names with 'js', so you can identify your JavaScript styles.

    For example, something I quite regularly do is add a 'jsEnabled' class to the parent of the elements that the JavaScript is working on.
blog comments powered by Disqus