Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change background color of table row (tr) when input is ":focus"?

Tags:

css

My HTML: <tr><td>Text</td><td><input type="text" value=""></td></tr>

My CSS: input:focus tr{ background-color:#fff;}

I want to highlight the row in white when I'm writing text in the input field. I know "tr" is before "input", but is this possible to do in any way?

Thanks a bunch

like image 711
Joel Avatar asked May 13 '11 02:05

Joel


People also ask

How do I change the background color of a row in a table?

The background color of the table is given by the bgcolor="color" attribute. When applied to the <table> tag, the color fills the background. Cell background colors are set by applying the bgcolor attribute to a <tr> tag (to color the row) or to a <td> tag (to color the cell).

How do you change the background color on TR?

HTML | <tr> bgcolor Attribute color_name: It sets the background color by using the color name. For example “red”.

How do you change the background color of a table row and column?

To change the background color of a single cell, use the attribute bgcolor="color" inside the <td> tag. To add a tiled background image to a single cell, use the attribute background="URL" inside the <td> tag.

How can you specify background color in a table?

The HTML <table> bgcolor Attribute is use to specify the background color of a table. Attribute Values: color_name: It sets the text color by using the color name. For example “red”.


3 Answers

No, sadly. See: Complex CSS selector for parent of active child

Here's how you could do it, though: http://jsfiddle.net/minitech/udzcp/

like image 85
Ry- Avatar answered Sep 20 '22 01:09

Ry-


Using JQuery, it is very possible. Observe:

HTML

<table border="1" cellpadding="20">
    <tr>
        <td>Text</td>
        <td height="50" width="100" id="somename"><input type="text" value="" id="mirza"></td>
    </tr>
    <tr><td>&nbsp;</td><td>&nbsp;</td></tr>
    <tr><td>a&nbsp;</td><td>1&nbsp;</td></tr>
    <tr><td>a&nbsp;</td><td>1&nbsp;</td></tr>
    <tr><td>a&nbsp;</td><td>1&nbsp;</td></tr>
    <tr><td>a&nbsp;</td><td>1&nbsp;</td></tr>
    <tr><td>a&nbsp;</td><td>1&nbsp;</td></tr>
</table>

CSS

.highlightedRow { background-color: orange; }

Jquery

$('input').focus(function() {
    $(this).parent().parent().addClass('highlightedRow');
});

$('input').blur(function() {
    $(this).parent().parent().removeClass('highlightedRow');
});
like image 20
Joshua Carroll Avatar answered Sep 19 '22 01:09

Joshua Carroll


Sadly, there's no way to style the parent element with CSS, so you'll have to use javascript.

like image 22
Emmett Avatar answered Sep 18 '22 01:09

Emmett