How can I select the closest HTML5 element with JavaScript?

We’re integrating analytics into an app, and we’re looking to grab the location of an element. In this case, there is no good div that gives a location, so we’re trying to grab the nearest html5 element.

Is this possible using .closest? Can I pass an array of tags [‘header’, ‘article’, section], and the nearest element that the clicked element is near is selected?

var tabs = Array.from(document.querySelectorAll('.details'));

function handleTabClick(e){

    var tabNode = e.target;
    tabLabel = tabNode.innerText;

   
};

tabs.forEach(function (tab) {
    tab.addEventListener('click', handleTabClick);
});

116 thoughts on “How can I select the closest HTML5 element with JavaScript?”

  1. Pingback: generic for viagra
  2. Pingback: sildenafil 20 mg
  3. Pingback: sale cialis
  4. Pingback: canada viagra
  5. Pingback: viagra alternative
  6. Pingback: cialis mexico
  7. Pingback: buy viagra
  8. Pingback: viagra vs.levitra
  9. Pingback: viagra bestellen
  10. Pingback: buying viagra
  11. Pingback: bestellen viagra
  12. Pingback: viagra users group
  13. Pingback: viagra gum
  14. Pingback: cheap viagra
  15. Pingback: cialis viagra
  16. Pingback: viagra amazon
  17. Pingback: sildenafil citrate
  18. Pingback: sildenafil goodrx