Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

jericopulvera's avatar

How to focus on an input after disabling?

I have this method

    comment (id) {
                    this.disable = true;
                    this.$http.post('/ajax/' + this.postId + '/comment', { body: this.body }).then((response) => {
                        eventHub.$emit('comment-added', response.body)
                        this.body = null
                        this.disable = false
                    })
               }

and this input

<input type="text" v-model="body" @keyup.enter="comment(postId)" :disabled="disable !== false">

how do I focus on the input after the input type gets enabled?

0 likes
6 replies
InaniELHoussain's avatar

using jquery, provid an id to the input

$("#myTextBox").focus();
1 like
WebKenth's avatar
document.querySelector('input').focus()

not sure how you find the right $el from your method

1 like
jericopulvera's avatar

@InaniELHoussain this works but when I submit a comment in the 2nd post component it it focuses on the first post component's comment form.

1 like
InaniELHoussain's avatar

here is it.

   comment (id) {
                  this.disable = true;
                  this.$http.post('/ajax/' + this.postId + '/comment', { body: this.body }).then((response) => {
                     eventHub.$emit('comment-added', response.body)
                        this.body = null
                     this.disable = false
            this.focus()
                    })
           }
1 like
prasinoulhs's avatar
Level 4

this.$el.querySelector('input').focus()

1 like

Please or to participate in this conversation.