Enhance the appearance of a different element when the input field is in focus

I am working with an HTML structure that looks like this:

<div class="floating-labels">
   <div class="form-group focused">
      <span class="select2 select2-container">
         <span class="selection">
            <span class="select2-selection">
                <ul class="select2-selection__rendered">
                    <li class="select2-search select2-search--inline">
                        <input class="select2-search__field" type="search"> // on focus here
                    </li>
                </ul>
            </span>
         </span>
         <span class="dropdown-wrapper" aria-hidden="true"></span>
       </span>
       <span class="bar"></span> // change style here                        
    </div>                                    
 </div>

My goal is to apply the style width: 50%; to the .bar class when the input box with class .select2-search__field is in focus using CSS.

I attempted to achieve this using the following code:

 .floating-labels > .focused > .select2-search__field:focus ~ .floating-labels > .focused > bar:after {
       width: 50%;
  }

Unfortunately, this approach did not work as expected.

Answer №1

If you wish to achieve this task without the reliance on Javascript, your best bet with your current HTML structure is to utilize focus-within. More information regarding this can be found on MDN, and the support for it is quite solid in modern web browsers.

Look out for focus-within on the .select2 span and target the subsequent .bar element's :after to apply your desired styles there. In case you need to cater to Internet Explorer as well, there are polyfills available, but then you'd end up resorting to Javascript anyway, so it might just be more straightforward to use Javascript to handle your styling.

.select2:focus-within + .bar:after {
  width: 50%
}

.bar {
  position: relative;
  display: block;
  height: 50px;
}

.bar:after {
  content: '';
  position: absolute;
  left: 0;
  top: 0;
  height: 100%;
  width: 5px;
  background-color: red;
  display: block;
  transition: .3s ease
}
<div class="floating-labels">
  <div class="form-group focused">
    <span class="select2 select2-container">
         <span class="selection">
            <span class="select2-selection">
                <ul class="select2-selection__rendered">
                    <li class="select2-search select2-search--inline">
                        <input class="select2-search__field" type="search">
                    </li>
                </ul>
            </span>
    </span>
    <span class="dropdown-wrapper" aria-hidden="true"></span>
    </span>
    <span class="bar"></span>
  </div>
</div>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Is it possible to change the background color using jQuery?

Can you assist me with this: I have a website and I am looking to modify the bg-coler when hovering over the menu (similar to how it works on the rhcp-website). I attempted using jquery for this, but unfortunately, it did not yield the desired result.. ( ...

Obtaining every PDF file linked within a designated div/table section of a website

Is there a way to download all PDFs linked in a table on a webpage using javascript? Edit: To clarify, I am trying to find a solution where Javascript can automatically download all the PDFs contained within a specific table on a webpage viewed in a brow ...

The challenges and benefits of using jQuery for background positioning compared to CSS animation positioning

I am currently developing a website where the main section has a series of divs with looping background images, and I am utilizing jQuery to animate their movement in order to create an illusion of an "ocean" of images flowing across the screen. let pos ...

Struggling to create a BMI Calculator using JS, the result is consistently displaying as 'NaN'

Having trouble creating a BMI Calculator using JavaScript. The issue I'm facing is that the calculation always returns 'NaN'. I've tried using parseInt(), parseFloat(), and Number() but couldn't solve the problem. It seems that the ...

Images are appearing misaligned in certain sections

I have been utilizing the freewall jQuery plugin for organizing images in my website. While the layout of my first section, located at , is functioning properly, I am encountering some issues with its height. The code snippet that I am currently using is a ...

Bundle NodeWebkit with a Web Server

I have developed an app that is now ready for distribution, and I have decided to integrate NodeWebkit into it. However, the challenge I am facing is that my app needs to load local files, requiring a web server. How can I combine NodeWebkit with a web s ...

What is preventing me from setting the height of the buttons to be the same as their width?

My website needs a panel on the left side of the screen that contains square buttons, each button representing a different action. However, currently these buttons are stretching horizontally when they should be stretched vertically to match my UI design: ...

Is there a way to access the variable value chosen from a select dropdown inside a function and then assign it to a JavaScript variable outside of the function?

Below is the HTML and JavaScript code that I am working with: function changeResult() { x = document.getElementById("dropdown-list").value; console.log((x)); } var qq; <select id="dropdown-list" onchange="changeResult()"> <option value="4 ...

The Multipart Parser encountered an unexpected end of stream error while in the starting state

i encounter this issue when attempting to submit a form link(rel='stylesheet',href='/stylesheets/home/profile/home_menu.css') script(type='text/javascript',src='/javascripts/perfil_editar.js') #logo_usuario img ...

Will my JavaScript function be triggered when the page is resized while printing?

I have a simple HTML layout where I am using the TextFill jQuery library to automatically adjust the text size based on available space. Everything looks good on screen, but when printed, it seems like the page is resized and the JavaScript needs to be re ...

Is it possible to modify an element within a list based on the identifier of its containing div?

I have created some additional software for my project, which includes a large div with smaller divs inside. Here is an example of how it is structured: <div class="scroll-area" id="lista"> <div class="box" id="item1"> < ...

How can I fix the "element is not defined" error in JavaScript when using Get

Having some issues displaying the #coupon using JavaScript, as I am encountering an error message stating that getElementById is undefined. Code Snippet (HTML) <html> <head> <title>Exercise</title> <link rel="styles ...

Tips for including an external babel JS (ES6) file in an HTML document:

To include the babel js file in an HTML file, take a look at the code snippet below: <html> <head> <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script> <script src="https://cdnjs.cloudfl ...

Can JavaScript be used to continuously monitor a window variable object in real-time?

Is there a way to dynamically control a variable in JavaScript? I'm currently working on a code that needs to display a button when it reaches the last signature of an automatic request process. The code for activating/deactivating the button is show ...

Issue with Navigation menu dropdown not functioning properly on Angular version 12 with Bootstrap 5

I am attempting to integrate bootstrap with Angular through a CDN. Below is the content of my index.html file: <!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <title>Laboratorio Sigma< ...

What is the best way to upload multiple files in Laravel using Guzzle?

Currently, I am facing an issue while trying to upload files to an external server using JWT for authentication. Here is a snippet of my form: <form action="{{ route('operations.photos-for-families.upload', [$operation, $current->index ...

Is there a way to include captions within a bootstrap carousel-item without compromising the responsiveness and alignment of the indicators and controls?

<div id="myUniqueCarousel" className="carousel slide" data-bs-ride="true"> <div className="unique-carousel-indicators"> <button type="button" data-bs-target="#myUniqueCarousel" data-bs-slide-to="0" className="active" aria-curre ...

How to Convert Pixel Units to Rem and Em in CSS?

Until recently, I developed my website exclusively using pixels, which has made it less responsive than I'd like. Is there a method to efficiently convert pixel measurements to rem/em units, allowing for quicker and easier modifications? ...

Is there a proper method for populating an HTML text with values from a form?

As a newcomer, I could really use some guidance here :) I'm trying to populate text with various words, numbers, and calculation results from a form. This is my initial attempt for just one field/word, and it appears to be functioning correctly. Do y ...

"Stunning Broth: Retrieve elements from a list without the need for comma

I am attempting to extract a list of cities from a webpage using Beautiful Soup The current output is: MonroeMatthewsWaxhawIndian Trail, Matthews What I want is: Monroe, Matthews, Waxhaw, Indian Trail, Matthews This is the HTML: <div id="current_tab ...