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

228
Vistas
How to replace a button element by its text, based on media query?

I simply want to remove the <button> element but keep the text in button at a max-width: 768px media query using JavaScript. I understand it is currently jQuery. I have it working to remove the button but cannot get the media query condition to work.

Here is the button, removed with JavaScript:

$(".rent").replaceWith(function() {
  return $(this).text();
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<button class="rent">Rent</button>

Here is what I tried to remove the button with a media query condition, but it does not work:

const mediaQuery = window.matchMedia('(max-width: 768px)');

if (mediaQuery.matches) {
  $(".rent").replaceWith(function() {
    return $(this).text();
  });
}

Here is update that half way works with event listener, but breaks on resizing back.

function checkMediaQuery() {
  const mediaQuery = window.matchMedia('(max-width: 768px)');
  if (mediaQuery.matches) {
    $(".rent").replaceWith(function() {
      return $(this).text();
    });
  }
}
window.addEventListener('resize', checkMediaQuery);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<button class="rent">Rent</button>

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

0

Your replace code should work, just depends on how and when you call it.

Although assuming you still want the button to do whatever it does, you could just style the button to look like text using css @media rules.

@media only screen and (max-width: 480px) {
  button.rent {
    background-color: transparent;
    border: none;
  }
}
<button class="rent">RENT</button>

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