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

277
Vistas
Hide specific HTML element if screen is smaller than 767px (jQuery)

On one of my websites I want to remove a specific HTML element if the screen is smaller than 767px (on mobile devices).

The following piece of HTML is used 9 times on the page:

<div class="price-list__item-desc">&nbsp;</div>

So all 9 pieces of HTML have to be removed only on mobile devices.

I already included the following jQuery file on the website:

jQuery(function ($) {
    if (window.matchMedia('(max-width: 767px)').matches) {
        $('<div class="price-list__item-desc">&nbsp;</div>').hide();
    }
    else {
        $('<div class="price-list__item-desc">&nbsp;</div>').show();
    }
});

However, the code is not working and the pieces of HTML still show up on mobile devices. I know there might be a very easy fix for this particular objective. But after searching the internet for two days I have come across so many different options that actually made me more confused.

Would very much appreciate any help, thanks in advance!

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

In the first method, when the page width changes, the div.price-list__item-desc element is hidden or shown using jQuery by controlling the page width.

In the method you developed, the anonymous function is not run to hide the item when the page changes; you need to use event handler method. In the structure I developed, the onresize event is affected when the page width changes. This way I can catch this event when the page width changes.

$( document ).ready(function() {
  window.onresize = function() {  
    console.log(`Width: ${$( window ).width()}`);
    var divs = document.querySelectorAll("div.price-list__item-desc");
    
    if (window.matchMedia('(max-width: 767px)').matches)
    {
        $('div.price-list__item-desc').removeClass("active");
      
        for (var i = 0; i < divs.length; i++) 
        {
          if(divs[i].innerHTML === '&nbsp;')
            divs[i].style.display = "none";
          else
            divs[i].style.display = "block";
        }
    }
    else
    {
      $('div.price-list__item-desc').addClass("active");
    }
  }
  
  window.onresize();
});
div.price-list__item-desc {
  background-color: red;
  display: none;
}

.active{
  display: block !important;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<div class="price-list__item-desc">&nbsp;</div>
<div class="price-list__item-desc">1</div>
<div class="price-list__item-desc">&nbsp;</div>
<div class="price-list__item-desc">2</div>
<div class="price-list__item-desc">&nbsp;</div>

about 4 years ago · Juan Pablo Isaza Denunciar

0

there's nothing calling your function. Use media queries instead.

UPDATE: I added some js to solve your problem. If you are trying to hide (so the div still takes up space but doesn't show anything) then your question doesn't really make sense. If you want to remove the div then set display to none. The js is used to grab the divs that you want

let divs = document.getElementsByClassName('price-list__item-desc')
for(let i = 0; i < divs.length; i++){
 
   if (divs[i].innerHTML == "&nbsp;")divs[i].classList.add('hide')
}
   
  
@media (max-width: 767px) { 

.hide{
display:none;}
}
<div class="price-list__item-desc">&nbsp;</div>
<div class="price-list__item-desc">&nbsp;</div>
<div class="price-list__item-desc">&nbsp;</div>
<div class="price-list__item-desc">&nbsp;</div>
<div class="price-list__item-desc">&nbsp;</div>
<div class="price-list__item-desc">6</div>
<div class="price-list__item-desc">7</div>
<div class="price-list__item-desc">8</div>
<div class="price-list__item-desc">9</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