Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

change style of button on focus of input box

Tags:

html

css

Problem: I want to change style of button on focus of input element.

Here is the template code:

<input type="text"></input>
  <span>
    <button class="search">Poor Me!</button>
  </span>

Here is the css code:

.search-form input:focus{
  border: 1px solid #009BC6;
  outline: none;
}

.search-form input:focus + .search{
  background-color: #009BC6;
  outline: none;
} 

Expected Output: On focus of input box
1. Apply border of 1x width to input box
2. Change background color of button

Actual Output:
1. Border is applied to input box
2. No changes in background color of button

http://jsfiddle.net/6Y8GL/7/

like image 489
Ajinkya Pisal Avatar asked Dec 01 '14 05:12

Ajinkya Pisal


People also ask

How do you focus a button in CSS?

The :focus selector is used to select the element that has focus. Tip: The :focus selector is allowed on elements that accept keyboard events or other user inputs.

How do you set focus on input field?

To set focus to an HTML form element, the focus() method of JavaScript can be used. To do so, call this method on an object of the element that is to be focused, as shown in the example. Example 1: The focus() method is set to the input tag when user clicks on Focus button.

How do I change input focus in bootstrap?

Answer: Use the CSS box-shadow Property By default, all the textual <input> elements, <textarea> , and <select> elements with the class . form-control highlighted in blue glow upon receiving the focus in Bootstrap.


2 Answers

demo -http://jsfiddle.net/6Y8GL/8/

input:focus ~ span #btnClick {
 background: red;
}

css cant find the #btnclick because it is not the sibling of the input you should be more specific, target the span and then #btnClick

input:focus {
  border: 2px solid green;
}
input:focus ~ span #btnClick {
  background: red;
}
<input type="text"></input>
<span>
    <button id="btnClick">Click Me!</button>
</span>
like image 111
Vitorino fernandes Avatar answered Nov 16 '22 00:11

Vitorino fernandes


Remove span around your button. The symbol + called next immediate sibling. According to your markup button is not next immediate sibling element.

<input type="text"></input>
<button class="search">Poor Me!</button>
like image 29
Suresh Ponnukalai Avatar answered Nov 16 '22 00:11

Suresh Ponnukalai