Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Is there a way to trigger a click event on a certain section?

I have found this post to be helpful but this is still not exactly what I am trying to accomplish: How do I handle a click anywhere in the page, even when a certain element stops the propagation?

I divided my page into sections so that you can vertically scroll through my horizontal pages. Each section takes up a full page.

I have a video on the 1st section & I want to trigger a click event that removes the video upon clicking anywhere on that page & jumps to section 2, which I achieved already with the below. However, this now triggers the page jump if I am clicking anywhere on my 6 pages & I just want this to trigger on the video page with section id="main"

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Is there a way to be even more specific? I tried to insert another location.href to the main page before/after the body in the 2nd line but this did not work.

video = document.getElementById('video')

document.body.addEventListener('click', () => {
  if (video.style.display === "block")  {
  } else {
    video.style.display ="none" && (location.href ="#About")
  }
},true);

>Solution :

You could check the id of the click-target before triggering the action, like so:

video = document.getElementById('video')

document.body.addEventListener('click', ({ target }) => {
  if (target.id === 'video') {
    video.style.display = "none";
    location.href ="#About";
  }
}, true);

Alternatively you could attach the click-handler on the element you care about and thereby avoid triggering the handler too often, like so:

document.getElementById('video').addEventListener('click', ({ currentTarget }) => {
  // `currentTarget` refers to the element the handler has been attached to
  currentTarget.style.display = "none";
  location.href ="#About";
}, true);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading