views:

2509

answers:

6

In my CSS I have a rule that must be applied to all text fields (using the CSS3 selector input[type=text].

I also use jQuery. Some browsers like Internet Explorer 6 does not support that form for CSS selector. So my workaround is to add an extra classname in CSS:

input[type=text], .workaround_classname{
  /* css styling goes here */
}

And via jQuery then add the CSS class:

$('input[type=text]').addClass('workaround_classname');

The Question is: How do I make sure this rule only is invoked when CSS3 selectors are not natively supported?

+2  A: 

Why not set some useless css style and check if the style was applied. Only add the workaround class to the elements that don't have this style applied.

Only problem is I can't come up with a "useless" style you can use :-)

Philippe Leybaert
I think the @conditional comments for IE6 can do the trick. (see my own answer below). This way, the styles can be applied to all text fields only -- and only if the browser does not support it natively. In a 8-10 years when IE6 is really phased out, then the @conditional comment can be removed.
Jesper Rønn-Jensen
+2  A: 

This seems to incorporate something like you speak of:

http://www.w3avenue.com/2009/07/02/detect-support-for-css3-html5-with-modernizr/

REA_ANDREW
A: 

Have a look at http://www.geocities.com/seanmhall2003/css3/detect.html. You should do this for each property.

Daniel Moura
A: 

There's no easy way to know "does this browser support CSS3 selectors". All you can do is detecting the browser and browser version.

Modernizr will not help you as you can't detect if a particular CSS selector works in a .css file. (Or you have to check if a particular CSS property from your CSS selector is applied and THIS is really ugly !)

BUT! Here the easiest way would be to forget about CSS 2.1 selectors like [attr=] in .css files as it's not widely supported. And if you have to write for browsers that doesn't support them (like Internet Explorer 6), you just can't use them.

So, add a class for everyone. Your code will be easier to understand, otherwise you'll always add unnecessary JavaScript code.

I understand I'm not answering you, but this is b-a-d!

vvo
A: 

I ended up using conditional comments in my javascript file. This way I could address IE6 and apply the CSS class

$(function(){
  //add css class to input[type=text] fields if the selector is not natively supported
    // IE6 hack: Since some CSS selectors are not working, we simulate with class names.
    /*@cc_on@if (@_jscript_version <= 5.6)
    $('input[type=text],input[type=password],input[type=radio],input[type=checkbox]').
        each(function() { $(this).addClass('type_' + this.type); });
    @end@*/

});

This way, I end up with added class names for all form fields:

.type_text{}
.type_radio{}
.type_checkbox{}
.type_password{}
Jesper Rønn-Jensen
really, doing this is actually worse than adding classes to elements for all browsers when you can't use some css selectors. Also, more javascript in ie6 means less performance ... :)
vvo
Vincent, you miss an important point: I want no extra work and no extra unnecessary steps performed by developers implementing my styles. Developers should just focus on thinking: "i want a text field here..." and NOT having to remember to add an extra `class="type_text"` or similar. I have seen that going wrong so many times.
Jesper Rønn-Jensen
Another thing that made me favor this idea is that there is absolutely no modification to existing fields. And absolutely no way of forgetting to set the classname.
Jesper Rønn-Jensen
A: 

Why not just have

input.workaround_classname{
  /* css styling goes here */
}

then add workaround_classname to all text inputs in your markup.

The selectors you could use are (I think the second is probably the one that would be easiest to understand what it was selecting for someone else reading the code)

$('input:text')
// or
$('input:text.workaround_classname')
// or
$('input.workaround_classname')

Then no detection is needed and you won't be relying on JavaScript to add the class name.

Russ Cam