Search
 
SCRIPT & CODE EXAMPLE
 

CSS

css focus input change another element

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.

function setFocused() {
  var results = document.querySelectorAll('.arbitrary');
  for (result of results) {
    result.classList.add('focused');
  }
}

function unsetFocused() {
  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>
  <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>
Expand snippet
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).
Comment

PREVIOUS NEXT
Code Example
Css :: canva 
Css :: html color codes 
Css :: use .map to count length of each element in an array 
Css :: Functions in Golang 
Css :: Responsive Web Design - Media Queries 
Css :: pixel css 
Css :: backface-visibility: hidden; 
Css :: button style css 
Css :: add tailwind to next 
Css :: sass example html 
Css :: advance css 
Css :: gradient 
Css :: css grid generator 
Css :: chrome close tab after print 
Css :: css aplying everything 
Css :: progress bar with width gradient 
Css :: hoq to give paragraph color css 
Css :: target id in media query 
Css :: if i forked and cloned a github repo can i change the name 
Css :: background affter layer css 
Css :: datatables when filtering snaps to top of page 
Css :: css style nth child beyond certain number 
Css :: How to hightlight source code using codemirror on a html page 
Css :: revert workflow version servicenow 
Css :: CSS REsize Wordpress Dropdowns Automatically 
Css :: countload on pageloading in html css 
Css :: sass @use 
Css :: easy way raise specificity css 
Css :: css safari webkit input search icon hide 
Css :: how to use class form scss reactjs 
ADD CONTENT
Topic
Content
Source link
Name
2+5 =