Investigating the problem with focusing on a textarea element nested inside a

I have a question regarding comments and replies.

Whenever I click the reply button on a comment, I want it to automatically focus on the collapsed textarea for that specific comment.

Since there are multiple textareas in the comment section...

  <div class="bg-white border shadow-soft p-4 mb-4" v-for="comment in comments" :key="comment.id" 

The reply button looks like this:

   <b-button class="text-action font-weight-light font-small" v-b-toggle="'collapse-'+comment.id" @click="focusTextArea('replay'+comment.id)">
                    <i class="fas fa-reply mr-2"></i> Reply</b-button>

The corresponding textarea is displayed below:

   <b-collapse :id="'collapse-'+comment.id">
     <textarea class="form-control " v-model="reply"  placeholder="Reply to John Doe" rows="6"  :ref="'replay'+comment.id"></textarea>
    <div class="d-flex justify-content-between mt-3"><small class="font-weight-light"><span >1000</span> characters remaining</small>
<button class="btn btn-primary btn-sm animate-up-2" type="button" @click="replyTo(comment)">Send</button>
   </div>
    </b-collapse>

In attempting to make the textarea focus when clicking the button, I have used this code:

   focusTextArea(textarea) {
                this.$nextTick(() => {
                    this.$refs[textarea][0].focus();
                });
            },

However, upon clicking the button, nothing happens. Instead, if I click the button again, the textarea collapses and the focus appears right as it is collapsing. Any ideas on what the issue might be?

Answer №1

I've taken the liberty to create a code sample in CodePen for you.

You're on the right track with your approach, but make sure to account for the animation of the collapse element before calling focus().

Check out my CodePen here:

https://codepen.io/uniquecoder/pen/XKFAJL

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

I'm struggling with my media query - it used to work perfectly, but now I can't figure out what went wrong

I seem to be having trouble with my media queries lately. They used to work fine, but now they seem to only render for specific screen sizes on certain pages. I'm targeting larger screens like (1920 x 1080) and (1680 x 1080). The code snippet is below ...

Tips for patiently anticipating an object to undergo asynchronous modifications?

I have an array containing items, and I need to incorporate the asynchronous value from getProductMinQuantity. The issue I'm facing is that the response with res.status(200)... gets sent before modifying item.order_quantity_minimum. I had assumed us ...

Use CSS to manipulate the dimensions of an overlay

Looking for a way to ensure that the height of your overlay matches the height of your carousel, even on smaller screen sizes? The HTML code below demonstrates how to combine simple HTML with a Bootstrap carousel featuring three images, along with an overl ...

Tips for synchronizing a session between a Vue.js developer and a Rails 4 backend

Currently, I am managing a Vue.js cli application alongside a Rails 4.2 backend. The main objective is to utilize the existing sessions. When I compile the Vue.js application for production mode, I can run the Rails server in development mode and serve th ...

What is the best way to locate a table of a specific class using jQuery selectors?

Is there a way to specifically target a table with the class "d" using jQuery selectors? I'm having trouble making it work... var dTableTags = $(".d table"); For instance, an example table would look like this... <table id="thetable" class="d"&g ...

Issue arises during initialization with Slick.js

I recently attempted to implement the slick.js function on my webpage by following the guidelines provided on . I copied the "center mode" function as my Jquery function, but unfortunately, the arrows/buttons and nav-docs were not generated in my slideshow ...

Determining the timestamp for when an AJAX request and response are executed

I'm looking to use a stopwatch to track the seconds elapsed between sending an AJAX request and reaching the success function. ...

Mobile devices are having issues with CSS rendering

I've encountered an issue with my CSS code. It seems to be working fine until the @max-width:767 media query, but then it stops working on the resolution immediately below it, which is @425. However, it starts working again on resolutions below that. ...

Tips for enhancing the "Eliminate render-blocking resources" score on the Lighthouse report for Progressive Web App (PWA) optimization

Check out my first attempt at a PWA here: My goal is to achieve a perfect 100% in Lighthouse for all categories :) https://i.sstatic.net/9Ql9r.png Although I've read the "Learn More" page, I'm still struggling with how to implement Bootstrap C ...

Jssor's dynamic slider brings a touch of sophistication to preview upcoming images

Incorporating the jssor nearby slider to create a nearly fullscreen display. The goal is to set the opacity of upcoming images to 0.25 when they are not in the main viewport, giving the edges of the upcoming and previous slides a slight transparency. < ...

Eliminate the use of ngTemplate in your AngularJS project

I just finished creating an AngularJS project with the yeoman generator, and now it's time to deploy it! After reading up on AngularJS, it appears that the yeoman generator creates Angular projects using templateCache. This means I need to utilize a ...

Attempting to deactivate the button across all pages within Datatables

Utilizing the Datatables JQuery plugin allows me to display data in a table format. In one column, I have incorporated buttons for editing rows. My aim is to make it so that when one button is clicked, the rest are deactivated. Below you will find my curr ...

Checking for the presence of the key name "item[]" within an object in AngularJs

I currently have an object called "obj" with two keys: "goal" and "item[]". It looks like this: var obj = {goal:"abc",item[]:"def"}; These keys and values are dynamically generated. Now here's the problem - I need to determine if these keys exist ...

What are some tips for keeping page content in place when the scroll bar appears?

Currently, I am undertaking a project focused on car travel at . However, I have come across an issue where the page content shifts to the left once a vertical scroll appears. How can this be prevented? My understanding is that one option is to keep the s ...

How can I bold just a single part of a string in VueJS?

I'm trying to figure out how to make a specific number within a string of text bold in Vue.js. Is there an easy way to achieve this? <a :href="detailUrl"> {{ content }} <span v-if="detailText && detailUrl" ...

CSS Shadow Text: Adding an Annoying Twist to Negative Space

Is anyone else experiencing the same issue as me with this "effect," or is it just happening to me? Check out this Fiddle Example Do you also see a small gray dot in the blank space? I've tried changing colors, fonts, and more but haven't been ...

finding the index of a particular checkbox in a list

I am working with a list of checkboxes that are dynamically created using JavaScript, each contained within its own separate div element. I need to determine the position number of a specific checkbox within all checkboxes sharing the same class within a p ...

When trying to change the audio source using jQuery, HTML audio is having trouble locating the

In my .html file, I have an audio element that looks like this: <audio id ="captcha"> <source id = "capt_wav" src = "doghouse.wav" type = "audio/wav" > </audio> It successfully plays the audio. However, now I need to dynamically ...

Determining the Missing Focus: Discovering the Vacant '":focus'" in Jquery

I am currently working on jQuery and facing an issue with input fields that have assigned ID's and attached .blur() events. I am struggling to identify the specific item that has gained focus: $("#"+field).blur(function() { console.log ($(this).attr ...

What is the best method for finding a button with an empty class name?

To access the search button on this page, follow this link: . Despite trying dr.findElement(By.cssSelector("button.button.cw-icon")), I was unable to locate the button labeled "搜索". Any suggestions on how to successfully locate it? ...