Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

221
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda