2

I have read about not being able to change the parent in CSS using pseudo classes such as :focus, so I am here to see if I am doing something wrong or if there is a javascript alternative.

I'd prefer not to use javascript at all though, if possible.

Here is my current code:

.box:focus .test{
background: #00FF00;
} 

So that code should change the background of the DIV .test to green when I click inside the textbox .box.

However this dosen't work, please could someone point me in the right direction, thanks.

Here is a code snippet of my HTML, this is a test page so I can see how this works.

<input name="textfield" type="text" class="box" id="textfield" />

<div class="test" id="test">Content for  id "test" Goes Here</div>
Gilles 'SO- stop being evil'
  • 104,111
  • 38
  • 209
  • 254
joshkrz
  • 499
  • 3
  • 7
  • 25
  • 3
    In javascript, yes, easily. Unfortunately, CSS has no access to the "previous" element or the "parent" element of the initial selection. – alt Aug 18 '12 at 22:35
  • If I were to use javascript how would I go about it? – joshkrz Aug 18 '12 at 22:39
  • 1
    It might be possible in [CSS4](http://www.w3.org/TR/2011/WD-selectors4-20110929/#subject) (- from [this answer](http://stackoverflow.com/questions/45004/complex-css-selector-for-parent-of-active-child/45530#45530)), but there is currently no support in any browser. – RichardTowers Aug 18 '12 at 22:40
  • Added jQuery method as answer. – alt Aug 18 '12 at 23:14

4 Answers4

3

In javascript, I'd install the jQuery library if you're goign to be writing a lot of it. Here's a bit of jQuery that'll change the parent css:

var parent = $(this).parent()

$('.box').focusin(function(){
    parent.css('background','#00FF00')
}).focusout(function(){
    parent.parent().css('background','transparent')
})
alt
  • 13,357
  • 19
  • 80
  • 120
3

This is actualy possible without using javascript. There is indeed no way to select a parent, but you can select a sibling using css. I set up a small example here: http://jsfiddle.net/k2dAp/1/

HTML

<div class='container'>
<input type='text' />
<div class='background'></div>
</div>

CSS

.container {
  border: red solid 1px; 
  position: relative;    
}
.background {
  position: absolute;
  background: grey;
  width: 100%;
  height: 100%;
  z-index: -1;
  top: 0;
  left: 0;   
}
input {
  margin: 10px;   
}
input:focus + .background {
  background: green;   
}

As you can see i added an extra div that will act as the background by adding absolute positioning and z-index. Then you can access it with the + selector. This will not be compatible with the old versions of IE but should work fine in modern browsers and degrade garcefully. Adding js will probably work in more browsers, but offcourse not for people with js disabled.

Pevara
  • 14,242
  • 1
  • 34
  • 47
2

can u give more details, like a code snippet or so...

js can do almost anything so the answer is yes there is an js alternative

mc_fish
  • 493
  • 3
  • 10
2

You can only do this via javascript like this using jquery

$("#textfield").click(function(){
$("#test").css('background-color','#333');
})
omoabobade
  • 515
  • 5
  • 16