I've done the part of adding the div's ID to the url, but after clicking the class speakers--overlay-close
in this speaker--card
I can't delete the button after I close it.
var myDivs = document.getElementsByClassName('speakers--card'); var closeDiv = document.getElementsByClassName('speakers--overlay-close'); for(var i = 0; i < myDivs.length; i++) { myDivs[i].addEventListener('click', function (event) { console.log(this.getAttribute("id")); let thisID = this.getAttribute("id"); window.location.href = window.location.href + "#" + thisID; }); }
I've tested some split functions etc but it doesn't work and the url remains the same - using #id
.
I ended up doing something like this, but not sure if this is the right approach:
Since the close button is inside the div, when you click it, the event propagates and runs the div's click event handler, which again adds the hash. You can stop event propagation by calling
event.stopPropagation()
in the close button event handler and then remove the hash from the url.