Jan. 19, 2009 at 2:35pmStyling Inputs is Hard

jQuery to the rescue!

Internet Explorer 6 is just too old to support all the fun CSS tricks of the modern web. The Input Type selector is one of the most problematic. I suggest a quick jQuery function to get around this.

Styling inputs is a pain. The input element has several different types, all of which are displayed in completely different ways. Briefly, they are:

  • Text
  • Password
  • Checkbox
  • Radio

As you can see, setting the width of all input elements will have unintended consequences for some input types. There are some common tricks to get around this. One is to add a special class to text and password boxes. Another is to use a CSS2 selector "input[type=password], input[type=text] { (css) }". The problem with this last method is Internet Explorer 6 doesn't support it. So what are we to do?

I created a script that will iterate though all input elements and append it's type to the class attribute. While this is a bit dirty, it does give us the ability to style these elements separately from each other without leaving IE6 in the dust.

$(function() {
$('input').each(function() {
        $(this).addClass(this.type);
    });
});

Two things to note after using this method now for a month or so:

First, the classes are added on load, so the inputs will be styled default until the page finishes loading. This also means that if you use the classes to select anything with jQuery, you have to wait until AFTER the function runs.

Second, to get around the jitter from loading, you can add CSS3 selectors input[type=text]. But these styles will be ignored by IE6, so be fair warned.

Left by Paul at SC | Mar. 3, 2009 at 4:48pm

Leave a Comment

Remember me

Name:

Email:

URL:

Comment: * No HTML, http:// will auto-link
* required
Comment Guidelines