3

Is there a way to change the colour of a row in a table when a checkbox in the row is checked using only CSS selectors?

I have the following, but it changes the colour of the checkbox, not the table cells:

.table_class tr td.column0 input[type="checkbox"]:checked { background-color:#f00; }
2
  • Essentially what you're asking for is a parent selector, which doesn't exist (yet) in CSS. The reason you're only able to change the color of the checkbox is because the snippet you posted targets/selects the checkbox, not the parent table cell. Commented Nov 1, 2010 at 20:20
  • That's what i figured :( Commented Nov 1, 2010 at 23:30

2 Answers 2

3

You could use a little bit of JS? This would act like a checkbox, without having to use a checkbox. DEMO

$("table tr").click(function() {
   $("table tr").css("background", "red"); //reset to original color
   $(this).css("background", "blue"); //apply the new color
});
Sign up to request clarification or add additional context in comments.

Comments

2

AFAIK, that's not possible with pure CSS to work out both conditions of checkbox. This won't work in IE < 9 either. You will have to resort to javascript for that.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.