Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

138
Views
Remove Click Event only in a specific browser view

I have a Toggle displaying content. I am trying to remove its click event in larger than 600px of browser view. That means the Toggle click functionality should not work in larger view of 600px. Following is the code i used.

HTML:

<div class="trigger">Trigger</div>
<div class="content">This is the Toggle Displaying content</div>
let trigger = document.querySelector(".trigger");
let content = document.querySelector(".content");

mobilefunction=(el)=>{
let cs = window.getComputedStyle(el).display;
if(cs==="none") {el.style.display="block"}
else {el.style.display="none";}
}

responsivemenu=()=> {
let windowwidth = window.innerWidth;
if(windowwidth < 500) {
    trigger.addEventListener("click", ()=>{mobilefunction(content)});
}
else {
    trigger.removeEventListener("click", ()=>{mobilefunction(content)});
}
}

window.addEventListener("resize", responsivemenu );
responsivemenu();

I am trying to make Click event enable only in mobile and remove it for desktop. Seeking for experts help on this. Thanks in Advance!

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

I'd recommend to not add/remove the eventlistener but just check inside the listener callback. By doing this you can drop all the resize handler stuff as the width is validated the second you click.

let trigger = document.querySelector(".trigger");
let content = document.querySelector(".content");

mobilefunction = () => {
  if (window.innerWidth >= 500) {
    return;
  }

  let cs = window.getComputedStyle(content).display;
  if (cs === "none") {
    content.style.display = "block"
  } else {
    content.style.display = "none";
  }
}

trigger.addEventListener("click", mobilefunction);
<div class="trigger">Trigger</div>
<div class="content">This is the Toggle Displaying content</div>

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!