Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

248
Views
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 answers
Answer question

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 Report

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!