how to not add id to url when click

Solutions on MaxInterview for how to not add id to url when click by the best coders in the world

showing results for - "how to not add id to url when click"
Lia
20 Nov 2016
1//Get all the hyperlink elements
2var links = document.getElementsByTagName("a");
3
4//Browse the previously created array
5Array.prototype.forEach.call(links, function(elem, index) {
6  //Get the hyperlink target and if it refers to an id go inside condition
7  var elemAttr = elem.getAttribute("href");
8  if(elemAttr && elemAttr.includes("#")) {
9    //Replace the regular action with a scrolling to target on click
10    elem.addEventListener("click", function(ev) {
11      ev.preventDefault();
12      //Scroll to the target element using replace() and regex to find the href's target id
13      document.getElementById(elemAttr.replace(/#/g, "")).scrollIntoView({
14          behavior: "smooth",
15          block: "start",
16          inline: "nearest"
17          });
18    });
19  }
20});
21