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

240
Vistas
How to add html code based on the screen sizes using Javascript?

I want to make add some code to the HTML dom according to the screen sizes. I want to totally remove the code when the screen size is something instead of still having the code and doing display: none. How can I do it?

<div id="main">
  <h1>One</h1>
</div>
const main = document.getElementById('main');

if(window.innerWidth <= 400) {
       main.innerHTML = '<h2>Added By js</h2>';
 }
 else {
 main.innerHTML = '';
 }
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

Simply use the @media screen of css, I think you need to do this:

h1{
   display: block;
  }
 h2{
   display: none;
  }

@media screen (max-width: 400px) {
 h1{
   display: none;
  }
 h2{
   display: block;
  }
 }

doing this you tell the browser to set display:none of the h1 when the screen width is over 400px, you can do it too with height

about 4 years ago · Juan Pablo Isaza Denunciar

0

If you want to do it with javascript, all you're missing are the events that will trigger your code

function yourFunction() {
    const main = document.getElementById('main');

    if(window.innerWidth <= 400) {
       main.innerHTML = '<h2>Added By js</h2>';
    } else {
    main.innerHTML = '';
    }
}

window.onload = yourFunction;
window.onresize = yourFunction;
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