Q: scrollIntoView in not working, why? and how to fixed? How can I make this code work? I'm getting errors
//scroll to section
this part is to get all the 
const linkes = document.querySelectorAll('a')

linkes.forEach(link => {

  link.addEventListener('click', toSection())

})

this part is to build the function for scrollIntoView
function toSection() {

  sections.forEach(section => {

    const sectionsIds = section.id

  })

 sectionsIds.scrollIntoView();

}


A: You can do it like this
<a data-myattr="myScrollSection1">to section 1</a>
<a data-myattr="myScrollSection2">to section 2</a>
<div id="myScrollSection1">Section1</div>
<div id="myScrollSection2">Section2</div>

<script>
  const links = document.getElementsByTagName("a");

  for (var i = 0; i < links.length; i++) {
    links[i].addEventListener("click", (e) => {
      myId = e.target.dataset.myattr;
      console.log(myId);
      document.getElementById(myId).scrollIntoView();
    });
  }
</script>

