Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

224
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda