user581733
user581733

Reputation: 504

Cannot read property 'scrollIntoView' of null - VueJS

I'm working in VueJS and I need to delete a topic and then scroll to the next item after the deletion was made.

Here is the code:

deleteTopic: function (index) {
                        var lcID="";
                        if(index===0) {
                            lcID = '#cAccordion-'+(index+1);
                        }
                        else {
                            lcID = '#cAccordion-'+index;
                        }
                        this.agenda.topics.splice(index, 1);
                        document.querySelector(lcID).scrollIntoView({ behavior: 'smooth'});
                        this.confirmDeleteTopicIndex = -1;                  
                    }

It's working perfectly EXCEPT if I delete the first item, that's when I get the error: Cannot read property 'scrollIntoView' of null.

In the code I'm checking for a zero index. It works...it will scroll but I'd like to get rid of the error.

Upvotes: 0

Views: 3536

Answers (2)

user581733
user581733

Reputation: 504

I set a timer as I said... here is the code.

setTimeout(function() {document.querySelector(lcID).scrollIntoView({ behavior: 'smooth'});}, 2000);

Upvotes: 2

user581733
user581733

Reputation: 504

I there. I placed a timer on it and it worked perfectly. The DOM had to update.

Upvotes: 0

Related Questions