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

219
Views
Onclick event for mobile devices using javascript matchMedia not working

Trying to make the onclick event work using javascript matchmedia for smaller screen devices. The mainappear() function outside alone without any matchmedia set works just fine. But I want that clicking on a div, making another div visible/hidden when the screen is smaller than 1000px. When I place the mainappear() function inside if condition, it does not work in resizing the window. The reviews-hover is the div where click will happen and reviews-main is the div that will be visible/hidden on click. How can I make the code work to solve this problem for small touch screens where onmouseover doesn't work ...? Don't want to use complex code. Can I make like my code work using javascript (it works without the matchmedia condition) ...?

function navclick(oiio) {
  if (oiio.matches) {
    function mainappear() {
      var revee = document.getElementById("reviews-main");
      
      if (revee.style.visibility === "visible") {
        revee.style.visibility = "hidden";
      } else {
        revee.style.visibility = "visible";
      }
    }

  } else {
    return;
  }
}

var oiio = window.matchMedia("(max-width: 1000px)")
navclick(oiio)
oiio.addListener(navclick)
<div id="reviews-hover" onclick="mainappear()">
</div>
<div id="reviews-main">
</div>

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

0

You should run mainappear function on every click and only toggle the visibility if the media matches.

var revee = document.getElementById("reviews-main");

function mainappear() {
  if (oiio.matches) {
    if (revee.style.visibility === "visible") {
      revee.style.visibility = "hidden";
    } else {
      revee.style.visibility = "visible";
    }
  }
}

var oiio = window.matchMedia("(max-width: 1000px)")
oiio.addListener(mainappear);
<div id="reviews-hover" onclick="mainappear()">first
</div>
<div id="reviews-main">second
</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!