2

I have a form such a structure:

<form role="search" method="get" id="searchform" action="">
    <input type="text" name="s" id="s" placeholder="Search..." />
    <input type="submit" id="searchsubmit" />
</form>

I need to when you click on the input field changed background submit button. How to do it?

2
  • And what was your attempt in this scenario? Any code you have produced? Commented Nov 25, 2014 at 8:08
  • So I tried: #searchform input[type='text']:focus #searchsubmit { background: url("../images/submit.png") 0 -14px no-repeat; } but it dont work Commented Nov 25, 2014 at 8:13

4 Answers 4

7

A pure CSS solution:

input[type="text"]:focus + input[type="submit"]{
    background-color: red;
}

Demo: http://jsfiddle.net/by10et61/

The + in the selector is what makes it work. The selector looks for a submit button, which is preceeded by a text input having focus.

Sign up to request clarification or add additional context in comments.

Comments

3

Like this:

$('#s').on('focus',function(){
  $('#searchsubmit').css('background-color','#f00');
}).on('blur',function(){
  $('#searchsubmit').css('background-color','');
});

Comments

1

Here is a working example that is easy and clear to understand

Markup

<form role="search" method="get" id="searchform" action="">
    <input type="text" name="s" id="s" placeholder="Search..." />
    <input type="submit" id="searchsubmit" />
</form>

Jquery

$(document).ready(function() {
  $('#s').on('focus',function(){
     $('#searchsubmit').css('background-color','red');
  });
});

Working Bin

4 Comments

Paste the code of your jsbin to the answer. Your answer will become useless if the link stop working, so make sure to put the relevant code as text in it.
Yeah it does, but what if someone in 6 months has this problem and google duides him here? Will the jsbin link work at this time too? You can´t be sure, so it´s recommended to put the code in the answer.
@DerVampyr I hear you,but was there any reason to down vote me?
The reason why you get downvotes is, because your answer is now ok, but maybe be bad in future, so it helps nobody. Just edit your answer and put the code from the jsbin in here and you will lose the downvotes, because your answer will become more useful. Useful link: How to answer on SO check the "Provide context for links" part.
0

You can try it with focus and out function both:

$('#s').focus(function(){
    $('#searchsubmit').css('background-color','#f00');
}).focusout(function() {
     //if you get back to other or previous...
    $('#searchsubmit').css('background-color','#ff0');
})

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.