Make :focus Change Css Of Another Class
Solution 1:
Using pseudo-classes (such as :hover
or :focus
) to modify other elements can only be done if the other elements are siblings or children of the element which has the pseudo-class. That's because CSS child/sibling selectors are fairly restrictive.
You can use the >
selector to select a direct child, and the +
selector to select a direct sibling. For example, if you have the following HTML:
<form>
<input type="text" />
<input type="submit" />
</form>
<p class="arbitrary">
This is an arbitrary element. It is neither a child nor sibling of
the text field. It cannot be selected as a result of a pseudo-class
action on the textfield using CSS, but can be selected using
client-side scripting such as JavaScript.
</p>
You could style the button when the text field has focus (because it is a direct sibling of the text field), but there is no possible way to style the arbitrary paragraph as a result of the text field receiving focus (because it is neither a child nor sibling, it is the sibling of a parent) without using client-side scripting (JavaScript, jQuery, etc.).
This CSS would style the submit button, and can be altered to select any direct or indirect child or sibling:
input[type="text"]:focus + input[type="submit"] {
/* some sweet CSS */background-color:green;
}
Using Javascript, of course, you have much greater flexibility. The focusin
and focusout
events can be used to toggle CSS classes. Here's an example that demonstrates both the CSS and JavaScript techniques of achieving this.
functionsetFocused() {
var results = document.querySelectorAll('.arbitrary');
for (result of results) {
result.classList.add('focused');
}
}
functionunsetFocused() {
var results = document.querySelectorAll('.arbitrary');
for (result of results) {
result.classList.remove('focused');
}
}
var results = document.querySelectorAll('input[type="text"]');
for (result of results) {
result.addEventListener("focusin", setFocused);
result.addEventListener("focusout", unsetFocused);
}
input[type="text"]:focus + input[type="submit"] {
/* some sweet CSS */background-color: green;
}
.arbitrary.focused {
/* even more sweet CSS */color: red;
}
<form><inputtype="text" /><inputtype="submit" /></form><pclass="arbitrary">
This is an arbitrary element. It is neither a child nor sibling of
the text field. It cannot be selected as a result of a pseudo-class
action on the textfield using CSS, but can be selected using
client-side scripting such as JavaScript.
</p>
Here's the jQuery equivalent of the above code, if that's your jam.
$('input[type="text"]').on('focus', function() {
$('.arbitrary').addClass('focused');
});
$('input[type="text"]').off('focus', function() {
$('.arbitrary').removeClass('focused');
});
Note that if you decide you want to do something similar, except using a "hover" trigger rather than "focus", you can use the JavaScript mouseover
and mouseout
functions, or the jQuery .hover()
function which takes two arguments (a handler for entering the hover and another for leaving the hover).
Solution 2:
Maybe add a ID
<div class="container">
<input class="myAwesomeInputBox"id='myAwesomeId'type="text">
</div>
and add and remove a class like this. Wont that solve your problem.
$('#myAwesomeId').on({
focus: function () {
$(this).addClass('focused');
},
blur: function () {
$(this).removeClass('focused');
}
});
CSS
input.focused {
border:3px solid blue;
}
Solution 3:
If the element css which you want to change is sibling, you can use like this,
<div class="container">
<inputclass="myAwesomeInputBox"><divclassName="dls-sibling"></div>
.myAwesomeInputBox:focus ~.dls-sibling {
&::before {
transform: scale(1);
border-color:red;
}
}
Post a Comment for "Make :focus Change Css Of Another Class"