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

252
Vistas
Deleting 5% of width every time the button is clicked JS

I don't know how to write a simple event listener to delete 5% od elements width every time the button is clicked. From 100% to 0%.

substracktBtn.addEventListener('click', function(){
    container.style.width = "calc(" + container.style.width + " - 5%)";
})

In this code i got no errors but the div's width is still 100% after the clicks.

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

0

This is your solution:

  • It will remove 5% of the beginning width each time till you reach 0%. With the other solution you will never get to 0% because you always take the 5% of the current width.

const container = document.querySelector('div')
const button = document.querySelector('button')
const subtractPerClick = container.offsetWidth * 0.05

button.addEventListener('click', () => {
  if (container.offsetWidth - subtractPerClick > 0)
    container.style.width = container.offsetWidth - subtractPerClick + 'px'
  else
    container.style.width = '0px'
})
div {
  height: 100px;
  width: 300px;
  background-color: darkblue;
}
<div></div>
<button>Substract</button>

about 4 years ago · Juan Pablo Isaza Denunciar

0

For removing 5% of the current width. This converges!

const container = document.getElementById('container');
const button = document.getElementById('button');

button.addEventListener('click', () => {
  container.style.width = container.offsetWidth * 0.95 + 'px'
})
#container {
  background-color: red;
  width: 200px;
  height: 100px;
}
<div id="container"></div>

<button id="button">Substract</button>

For removing 5% of the starting width

const container = document.getElementById('container');
const button = document.getElementById('button');

let startWidth5Percent = container.offsetWidth * 0.05;

button.addEventListener('click', () => {
   container.style.width = container.offsetWidth - startWidth5Percent + 'px';
})
#container {
  background-color: red;
  width: 200px;
  height: 100px;
}
<div id="container"></div>

<button id="button">Substract</button>

about 4 years ago · Juan Pablo Isaza Denunciar

0

How about just:

substracktBtn.addEventListener('click', function(){
    container.offsetWidth = container.offsetWidth * 0.95
});

Note: untested, not sure if JS requires the 'px' added at the end, but you get the gist.

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