views:

84

answers:

3

For example, I want to make two textboxes have the same style when either is focused:

<div class="divTxt">
    <input type="text" id="a" class="a" />
    <input type="text" id="b" class="b" />
</div>

and the css would be:

.a:focus 
{
    background-color:Blue;
}
.b:focus 
{
    background-color:Yellow;
}

What I need is make a's background-color:Yellow when b is focused and vice versa. any possibilities? Thanks a lot.

+3  A: 

You could try the General Sibling Selector(~) if the input boxes are next to each other.

Something like:

.a:focus {    background-color:Blue;}
.a:focus~.b {    background-color:Blue;}
.b:focus {    background-color:Yellow;}
.b:focus~.a {    background-color:Yellow;}

Note: Completely untested and a stab in the dark at best!

Jon P
Good point +1. A pity this is not well implemented in older browsers.
o.k.w
Not to mention buggy in IE7, the linked article mentions it's compatability issues.
Jon P
thanks, but this is not used by older browsers... And there are still a large number of people who use the old ones.
Jronny
change .a:focus~b to .a:focus~.b... this is not working when b is focused. Thanks a lot anyways. =)
Jronny
@Jronny, with those constraints I think you could be stuck. If a solution can be found, they will certainly get my vote!
Jon P
+1  A: 

If they've got javascript disabled, they probably won't notice text box styles.

carillonator
well, it's their fault they disabled javascript anyways. They would not notice the beauty of my creation. *evil laugh*
Jronny
A: 
.chk1:focus{
   background-color:Blue;
}
.chk2:focus{
   background-color:Yellow;
}

text feilds

<input class=chk1 type=text id="a">
<input class=chk2 type=text id="b">

This one will work fine with FireFox but might have issues with IE6 See CSS Issue Focus For IE6

If you want this to work with IE, you might want to use javascript!!

Hope this helps

RDJ

Richie