0

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.

3
  • Just debug the code. Most probably there is no element in the DOM with one of the ids set by lcID Commented Feb 26, 2020 at 15:38
  • You could also make use of references into your template ! vuejs.org/v2/api/#ref This way you would be able to check the existence of your dom element in an easier way Commented Feb 26, 2020 at 15:44
  • I am trying to debug the code. It should work. as there will always be a '#cAccordion-'+(index+1); I'll keep trying :-) I will also look up $refs. Thanks. Commented Feb 26, 2020 at 15:47

2 Answers 2

2

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

setTimeout(function() {document.querySelector(lcID).scrollIntoView({ behavior: 'smooth'});}, 2000);
Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.