How can you apply styling to one element when focusing on a different element at separate hierarchy levels?

I'm currently facing a challenge with styling a text field to expand when focused, and adding an "Add" button below it. The issue is that the elements are on different levels in the code structure, making it difficult to show or hide the button when focusing on the text area. Here's a snippet of the current setup:

<form class='container'>
   <div class='form-item'>
      <div class='input-container>
         <textarea id='addComment'></textarea>
      </div>
   </div>
   <span class='button-wrapper'>
      <button id='addCommentBtn'></button>
   </span>
</form>

Here is the CSS/SCSS I've been using:

#addCommentBtn {
    display: none;
}

#addComment {
    transition: all 0.5s ease;
    margin: 0.5em;
    width: 95%;
}

#addComment:focus {
    height: 10em;
    margin-bottom: 2em;
}

#addComment:focus + #addCommentBtn {
    display: block;
}

While the textarea expansion on focus is working correctly, toggling the button from display:none to display:block isn't functioning as expected. I have tried various solutions like adjusting visibility property but haven't had success.

If necessary, I might need to make changes to the Vue components, although this would require approval and could impact other parts of the project due to component reuse.

In addition, I am avoiding the use of JQuery for this task as well.

Answer №1

This solution addresses the issue by utilizing Flex to dynamically adjust the container's height based on its content.

function toggleButton(displayFlag) {
  const btn = document.getElementById('addCommentBtn');
  btn.style.display = displayFlag ? "inline" : "none";
}
.container {
  display: flex;
  flex-direction: column;
}

#addCommentBtn {
  display: none;
}
<form class='container'>
   <div class='form-item'>
      <div class='input-container'>
         <textarea id='addComment' onfocus="toggleButton(true)" onblur="toggleButton(false)"></textarea>
      </div>
   </div>
   <span class='button-wrapper'>
      <button id='addCommentBtn'>Add</button>
   </span>
</form>

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

There is a lag in the loading of css stylesheets

We created a single-page company website that utilizes three different stylesheets connected to the index.html. By clicking a button, users can switch between these stylesheets resulting in changes to colors, images, and text colors. However, Issue 1: The ...

Is it possible to have evenly spaced divisions within a fixed wrapper?

I have been experimenting with creating a dynamic navbar that remains at the top of a webpage. Here's what I have so far: <style> .sticky-nav-wrapper > div { color: #000000; display: inline-block; display: -moz-inline-box; * ...

Using an inline-block within a positioned absolute element

I am curious about the behavior of inline-block elements inside absolutely positioned elements. To better explain, I have provided an example below. We can see in the code snippet why the .icon within the .tag is not displayed like the previous .icon (whic ...

Developing a personalized Markdown-it extension for a Nuxt web app causes a Type Error while displaying in a web browser

I have been working on developing a Nuxt.js application utilizing markdown rendering with markdown-it. To achieve this, I created a custom plugin located in the "helpers" directory. nuxt.config.js ... modules: [ ..., '@nuxtjs/markdownit', ] ...

Oops! Looks like there was an error: weight is not defined when trying to calculate BMI with the HTMLButtonElement

I'm currently working on a project to create a BMI calculator following specific instructions. I've managed to follow all the guidelines except one regarding the letsCalculateBMI function. The instruction states: letsCalculateBMI should extrac ...

The pug blend fails to produce the desired output

Recently, I've encountered an issue while working with Pug in a Vue.js application. The task at hand is to create a multi-level menu (with submenus) using the provided data structure: mounted () { this.catalog = [ { title: "Компр ...

If an element with a "hidden" display property is loaded in the browser window, will it be visible?

Is an element in a hidden display still using memory when the page is loaded? It's convenient to have many elements on a page, but if 99 elements are hidden and only 1 is displayed, does that impact the loading of the page? I'm curious if the pa ...

How can I align bullets in an unordered list with the left margin of the heading above?

Is there a way to line up the bullets with the left margin of the text above the list? Here's an example: <html> <style> ul.p1 {padding-left: 40px} ul.p2 {padding-left: 0px} </style> <body> <h2&g ...

Change button to an ajax spinner when it is clicked using jQuery

$(".post-btn").html("<img src='../images/loader.gif' />"); Why isn't this code working? I know I have the correct selector because when I tried $(".post-btn").text('test'), it worked. I want the text of the button to change ...

Is there a way to eliminate the transform style from a draggable element?

I am looking to enhance the CDK drag and drop example by adding a preview of the dragged element with specific left and top positions, without utilizing the transform style. HTML <div class="example-boundary"> <div class="example- ...

Encountering a 400 Status Error in Shopware stating "The value provided is too lengthy. Please limit it to 255 characters or less" when attempting to update a database table

Whenever I attempt to insert data into the Shopware 6 database table, I consistently receive a 400 status response along with the message This value is too long. It should have 255 characters or less. The field in question that I am trying to update, desc ...

A warning message from Laravel: "Template compilation error in Vue"

I'm experiencing an error when compiling Vue. I've added the following code snippet at the bottom of the page: <script src="{{ asset('js/app.js') }}"></script> <script type="text/javascript"> @yield(& ...

Vue alert - Cannot access indexOf property of a value that is undefined

After browsing through numerous similar issues on StackOverflow, none seem to address the specific problem I encountered... I have been smoothly working on a Vue JS project + Laravel until I suddenly encountered an error that seems unsolvable (even though ...

Dynamic Binding of Checkboxes in Vuex

I am encountering a problem with binding checkboxes using Vuex. Even though I am using v-model with a variable that has a getter and setter to set or get the value in the store, I keep getting incorrect data in the store. The issue arises when I click on a ...

Problem arises when the DIV elements are not expanding horizontally together

Struggling to create a round corner box using divs. I have been working on it all day and can't figure out what I'm missing. If anyone could help me spot the issue, that would be great. I want the 'header' and 'footer' to exp ...

Changing the input to uppercase within Vue.js

I am looking to capitalize the input data from the user's full name model and would prefer if it is in Latin letters only. Utilizing Vue.js for Uppercase Conversion <p-input :value="value" @input="$emit('input', $event)&qu ...

The DIV refuses to center-align

I am struggling to center a div element. I have tried using margins (margin: 0 auto) and left/right CSS attributes (left: 50%, right: 50%), but the result is not as expected. Can anyone point out where the problem might be? EDIT: The issue I'm facin ...

Solution for tables extending beyond the navbar and footer issue

My table is extending beyond the width of both the navbar and footer. Is there a way, using HTML and CSS, to make the navbar and footer expand in width to match that of the table? Most solutions I've found focus on formatting the table instead of addr ...

I'm encountering an issue where Bulma is taking precedence over the CSS of my Vue3

In my Vue CLI 3 project, I'm utilizing Bulma and have included the import in the 'index.js' file like so: import { createApp } from 'vue' import App from './App.vue' import router from './router' require('@ ...

Vue Internationalization for multiple languages

While implementing multi-language support in vue js, I encountered an issue where the data in the menuItem name does not update when changing the language. Despite everything else working fine, this specific aspect seems to remain static. Explore Vuei18n ...