tags:

views:

373

answers:

7

How do you select a radio button in CSS? The HTML I am working with is generated so I cannot add class or other attributes to it.

I found input[type="radio"] on the internet, but I don't know if this is regular CSS and supported by most browsers.

Is there any other ways to select a radio button?

Thank you,

Brett

+5  A: 

input[type="radio"] is an example of an attribute selector. It's part of the CSS3 spec and is perfectly legal. The only browser that doesn't support them is IE6. If supporting IE6 is important to the project, then you should look into adding classes to the radio buttons in question.

Here's an article with an example of how to effectively use attribute selectors. Check out this article for more info on CSS3 goodies.

Evan Meagher
What is your first URL (link) referring too?
Brettski
That would be me copying the wrong link. Thank you for the heads-up.
Evan Meagher
This isn't a CSS3 goodie -- it's actually part of CSS2. So IE6 has no excuse for its lack of support.
Bennett McElwee
A: 

The obvious, portable way to do it is: Give it a class, use that.

<input type="radio" class="radio" ...>

and

input.radio {
  ...
}
Tomalak
Down-voted because? It's pragmatic, it works (even on IE6) and it answers the question.
Tomalak
Oh, I overlooked the "HTML is generated" part. Okay, the down-vote is appropriate.
Tomalak
Yeah, I had stated I couldn't use a class or other attribute. thank you for the response though.
Brettski
+8  A: 

The attribute selector (input[type="radio"]) is the correct solution, widely supported by everything but IE6 :)

If you have no ability to modify the HTML to inject classname support (or access to javascript to accomplish this) then your options are:

A). to make sure your site doesn't depend on this, and allow IE6 to degrade gracefully.

B). live without it

annakata
A: 

Your solution is the correct one and will work in all modern browsers apart from IE6. For IE6 you'll have to either find way of selecting them, modify the HTML or use Javascript.

edeverett
+1  A: 

The one you mention above is the correct way to target it by CSS. It's called an attribute selector. It is not supported by IE6 and below however. They can be simulated by adding a conditional behaviour script for IE6. (Just search on google, the file ending is usually .htc).

It should probably be noted that .htc-files usually result in horrendous performance and should only be used sparingly and be thoroughly tested to ensure that the performance hit is acceptable.

PatrikAkerstrand
egad, .htc voodoo! Good thinking though on alt solutions.
annakata
+3  A: 

you could use jQuery to select the input and add a class dynamically.

Example (source: http://docs.jquery.com/Attributes/addClass#class):

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
                    "http://www.w3.org/TR/html4/loose.dtd"&gt;
<html>
<head>
  <script src="http://code.jquery.com/jquery-latest.js"&gt;&lt;/script&gt;

  <script>
  $(document).ready(function(){
    $("p:last").addClass("selected highlight");
  });
  </script>
  <style>
  p { margin: 8px; font-size:16px; }
  .selected { color:red; }
  .highlight { background:yellow; }
  </style>
</head>
<body>
  <p>Hello</p>
  <p>and</p>
  <p>Goodbye</p>
</body>
</html>

[Edit]

an alternative to jQuery is to use http://code.google.com/p/ie7-js/

it fixes loads of issues with ie versions lower than 7 the fix that will interest you most is illustrated here:

http://ie7-js.googlecode.com/svn/test/attr-value.html

Josh
ah exactly like that :)
Tom
Yeah, I like that idea, but I have to support IE 5.5 on this project :(
Brettski
A: 

You could use jQuery to find all the radio buttons on the page, then add some CSS classes to it.

Tom