Topic: MDB5 SmootScroll throws error if called by JS
jczeledonp free asked 3 years ago
Expected behavior activate SmootScroll by javascript:
new mdb.SmoothScroll(document.getElementById('idname'))
Actual behavior
Uncaught TypeError: can't access property "includes", this._href is null
Resources (screenshots, code snippets etc.) This procedure is explained at https://mdbootstrap.com/docs/standard/methods/smooth-scroll/#docsTabsAPI
Example snippet: https://mdbootstrap.com/snippets/standard/juan_carlos_zeledon_paniagua/2938470
jczeledonp free answered 3 years ago
Hi Grzegorz,
Thanks for the code but it doesn't working by JS. The scroll works because you are clicking on an anchor element and it's taken the attributtes.
Can you make a JS example, please? I updated my snippet taking the instance of anchor element, not the target but it still not working.
Thanks!
Grzegorz Bujański staff commented 3 years ago
It doesn't work that way. Without initialization via JS, my example will not work. Remove the js code from my example and you will see that the example stops working.
Your example won't work the way you want it to work. At this point, you are trying to create a new class instance on the element where the class instance already exists. But this will not make the page scroll to the right place when you click on this button.
You could change the code so that the id refers to the Smooth Scroll by JS
button,
but this will cause the first click initiate a smooth scroll, and the next click scroll the page. Therefore, this is also not a good solution.
Grzegorz Bujański staff answered 3 years ago
The id should be added to the item you are clicking on, not the target item. I have prepared a working example: https://mdbootstrap.com/snippets/standard/grzegorz-bujanski/2944560#js-tab-view
FREE CONSULTATION
Hire our experts to build a dedicated project. We'll analyze your business requirements, for free.
Answered
- ForumUser: Free
- Premium support: No
- Technology: MDB Standard
- MDB Version: 3.2.0
- Device: Laptop
- Browser: Chrome, Firefox
- OS: Windows 10
- Provided sample code: No
- Provided link: Yes