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

How can I tap into the page reload event in Nuxt.js?

One challenge I'm facing involves the need to trigger a function each time the user refreshes the page. Typically, this could be achieved by utilizing the created hook in App.vue. However, due to the auto-generated base component in this project, impl ...

What is the best way to shorten string values that exceed a certain length?

Here is an array list I have: $myarray = array( array( 'name' => 'File name 1 - type.zip', 'size' => '600KB', ), array( 'name' => 'File name 2 - type.pdf&a ...

What could be causing my transform scale to not function properly in Bootstrap?

As a complete beginner, I am currently in the process of learning bootstrap. I have noticed that the animation breaks when I apply it, specifically the scaling part. Interestingly, the animation works perfectly fine without Bootstrap. I am wondering if the ...

Dropdownmenu without borders

I'm having an issue with the dropdown menu on my website - there are no borders showing. I've tried fixing it myself with no luck, so I could really use some help. As a beginner in web development, I don't have much knowledge about these thi ...

Using Laravel along with Inertia.js and Vue, we can easily determine whether a user is logged in

I am currently using Laravel along with Inertia.js for my project implementation. I am facing an issue where I want to display a div element containing some user details in the navbar only if the user is logged in. However, the div should not appear if the ...

Passing a variable to the render method in a PDF document

I'm currently working on a project with Django where I need to convert an HTML monthly report into a PDF. The data for the report is retrieved from a database based on the selected month. I am facing an issue in sending the selected month from the HTM ...

The battle of line-height versus padding for achieving vertical centering with one-lined text

One-lined text can be vertically centered using either the line-height property or by adding padding-top and padding-bottom. What are the advantages and disadvantages of each method? body { font-size: 12px; font-family: ...

Ajax TabContainer mysteriously vanishes during Postback

I'm currently working on a GIS web application using C# ASP.net. Within my project, I have an Ajax TabContainer that houses multiple TabPanels containing various elements like a map window and scale bar from the ESRI WebAdf toolkit. Below is a simpl ...

Adjust the appearance of elements depending on whether the user has activated dark mode or

What is the most efficient way to implement both dark and light themes in my Vue app? One option is to create a 'dark.scss' file and use the '!important' property to override styles defined in components. Another option is to utilize pr ...

Access SCSS variable values in Angular HTML or TypeScript files

So, I've been looking into whether it's feasible to utilize the SCSS variable value within HTML or TS in Angular. For instance: Let's say I have a variable called $mdBreakpoint: 992px; stored inside the _variable.scss file. In my HTML cod ...

Troubleshooting: Inline Styles not displaying Background Div Images in Next.Js

I am currently attempting to display images in a component by using a map and an external JS file to store the images as objects. I then loop through them to set each one as a background image for every created div. However, it seems like my current implem ...

Steps to conceal an accordion upon loading the page and reveal it only when clicking on a specific element

Below is the code I am using to display an accordion in a Fancybox popup. However, I do not want the accordion to be visible on page load. If I hide it, the content inside also gets hidden when showing the accordion in the popup. When user clicks on Click ...

While v-carousel adjusts to different screen sizes, the images it displays do not adapt to

Whenever I implement v-carousel, everything seems to be working well, but there is an issue on mobile. Despite the carousel itself being responsive, the images inside do not resize properly, resulting in only the center portion of each image being displaye ...

I am curious if there is a method to vertically center text without being affected by the font-family or font-size

Searching for a font-independent method to center text within a div. My div has a button-style fixed height, and I need the text (one line only) to be centered vertically within it. The issue arises when changing the font-family, as some fonts do not ali ...

Swap out the string variable when it is modified

To generate a string inside the "code" variable that combines the selected image values, the final code should appear similar to: "test1/A=1a/B=1b" or "test2/A=1b/B=1a", etc. If the user modifies icon "A," it should replace the value in the code instead of ...

Having trouble with Laravel, Vue.js, and Pusher not communicating with a private channel on Heroku

I have a Laravel/Vue.js real-time application that works perfectly on my local environment. However, when I deploy it to Heroku, the response from /auth/broadcast is unusual. bootstrap.js window.axios.defaults.headers.common = { 'X-Requested-With& ...

Having trouble linking a sqlite file in your tauri + vue project?

After successfully installing tauri-plugin-sql by adding the specified content to src-tauri/Cargo.toml : [dependencies.tauri-plugin-sql] git = "https://github.com/tauri-apps/plugins-workspace" branch = "v1" features = ["sqlite" ...

Discover the Vue.js component names in this array list-based show

I currently have multiple components in my code, which has made it quite lengthy. I believe there is a way to simplify it. Is it possible to structure it like the following example code: Where the component names are referenced dynamically based on data? ...

The beauty of Aurelia's scoped CSS

Embarking on my Aurelia journey, I find myself navigating through the vast world of web development with tools like nodejs, gulp, and more. The Aurelia CLI has made it convenient for me to set up a visually appealing Aurelia project in Visual Studio Code ...

Can applications on Windows 8 operate using JavaScript, HTML5, and CSS3 that adhere to industry standards?

As a .NET developer, I tuned in to the keynote for the Build Event in Anaheim, California and had some questions regarding the new support for creating Windows 8 applications using JavaScript, HTML5, and CSS3. They showcased several examples and mentioned ...