how to not add id to url when click
//Get all the hyperlink elements
var links = document.getElementsByTagName("a");
//Browse the previously created array
Array.prototype.forEach.call(links, function(elem, index) {
//Get the hyperlink target and if it refers to an id go inside condition
var elemAttr = elem.getAttribute("href");
if(elemAttr && elemAttr.includes("#")) {
//Replace the regular action with a scrolling to target on click
elem.addEventListener("click", function(ev) {
ev.preventDefault();
//Scroll to the target element using replace() and regex to find the href's target id
document.getElementById(elemAttr.replace(/#/g, "")).scrollIntoView({
behavior: "smooth",
block: "start",
inline: "nearest"
});
});
}
});
Also in JavaScript:
- javascript push and concat
- import img react
- mod remainder js
- angular input force uppercase
- javascript reduce sum
- javascript indexOf object value in array
- javscript insert element after another
- return this javascript
- username validation js
- defer accessing a variable until available in js
- aos js cdn
- plus minus hackerrank solution javascript
- javascript datetime
- javascript for loop return index
- busca binária em javascript
- compare string length javascript
- array includes
- javascript remove item onclick
- localecompare javascript
- digit count in javascript
- using slice and reduce together in javascript
- device height react native
- bookshelf insert multiple rows
- JS get viewport width