1

Small question but, cannot find method to solve this little problem. I have html form

 <div id="todolist">
      <span class="add-on">OK</span>
      <input class="addtodo" placeholder="New todo task" name="TITLE"  type="text" >
 </div>

in CSS

 #post-todo span{   
     color:#aaa;
 }

I want to change color:#333 when focused on input, how do this?

brasofilo
  • 25,496
  • 15
  • 91
  • 179
user3054630
  • 334
  • 2
  • 5
  • 14
  • It's possible with pure css, but only if you put the span after the input.Check http://stackoverflow.com/questions/6910049/on-a-css-hover-event-can-i-change-another-divs-styling – erdimeola Dec 14 '13 at 13:34
  • Okay interesting, didn't know that method, even though its kind of logical. But its not Browser compatible so unfortunately useless in production. – greenish Dec 14 '13 at 13:42

3 Answers3

3

use CSS:focus selector something like below.

     input:focus
     {
        background-color:yellow;
        color:blue;
     }

For suppose your OK span would be placed after the input then the below code will work without the help you of the jQuery. Only the CSS will do the trick.

<div id="todolist">
      <input class="addtodo" placeholder="New todo task" name="TITLE"  type="text" >
      <span class="add-on">OK</span> <!-- span must be after input-->
 </div>

 .addtodo:focus + .add-on
 {
    background-color:yellow;
    color:blue;
 }

The + in the CSS is used to Matches any element immediately preceded it by a sibling element.

Consider

E + F
{
    // code
}

Then Matches any F element immediately preceded by a sibling element E.

Here is the Demo http://jsfiddle.net/UxZXN/

Alex KeySmith
  • 16,657
  • 11
  • 74
  • 152
Gourav
  • 1,765
  • 2
  • 15
  • 16
2

Unfortunately it is not possible with pure css to change the span styling when focussing the input. Using the :focus selector works only for child elements of the focused one.

But there is a fairly simple javascript jquery method :

$("#post-todo .addtodo").focus(function(){
    $("#post-todo .add-on").css("color", "#333");
})

$("#post-todo .addtodo").blur(function(){
    $("#post-todo .add-on").css("color", "#aaa");
})

See here: http://jsfiddle.net/BEeNa/

J0B
  • 1,648
  • 1
  • 12
  • 24
greenish
  • 1,042
  • 1
  • 8
  • 18
1

It finally become possible, just a few years later...

It is possible with support of :focus-within.

#todolist:focus-within .add-on { color: #aaa; }
<div id="todolist">
  <span class="add-on">OK</span>
  <input class="addtodo" placeholder="New todo task" name="TITLE"  type="text" >
</div>
tsh
  • 4,263
  • 5
  • 28
  • 47