troubleshooting Question

scrollIntoView(); trouble getting it to work

Avatar of Mark Bran
Mark Bran asked on
JavaScript
8 Comments1 Solution48 ViewsLast Modified:
Here is a code pen with my code
https://codepen.io/jsMarko/pen/c115a9b2edc350fe342c9e2b365d6300?editors=1111
Having trouble in my js

Essentially I am using Template Literal to create anchor buttons that when clicked should scroll to the respective section. However, in the test code in above codepen is not working
I just cant seem to see why its not scrolling
Also note the e.preventDefault is not working either as it is still cycling through the sections because of the Anchor href pointing to them

Here is the area I think is the concern const evListen = document.querySelector('#contactLink'); // getting contactLink

const evListen = document.querySelector('#contactLink'); // getting contactLink
evListen.addEventListener('click', (e) => {
   e.preventDefault();
   let contactEv = document.querySelector('#contact');  
   contactEv.scrollIntoView();
   console.log('It clicked'); // It clicked is working
   });
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 8 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 8 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros