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/
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.
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.
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.
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>
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With