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

228
Views
How to change height of box together with its width?

I want to publish on my page section with articles in rows if form of boxes. I use bootstrap grid system to make it responsive. I set width of boxes to be 100%. And I want height to be as the same as width. For this I wrote an javascript code to execute this task:

document.addEventListener("DOMContentLoaded", function(event) { 
    let width = document.querySelectorAll(".post-boxes .box");
    width.forEach(item =>{
    item.style.height = `${item.offsetWidth}px`
    });
  });

But the problem is it works only when DOM is loaded for the first time, and if screen size is change, height remain the same.

This is my photo of boxes:

IMG

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

One solution is to run the function again when the screen is resized. You can use the resize event for this. Here is an example:

function box(){
    let width = document.querySelectorAll(".post-boxes .box");
    width.forEach(item =>{
    item.style.height = `${item.offsetWidth}px` })
}

document.addEventListener("DOMContentLoaded", () => box() );
window.addEventListener("resize", () => box() );
about 4 years ago · Juan Pablo Isaza Report

0

DOMContentLoaded event fires when the initial HTML document has been completely loaded and parsed, without waiting for stylesheets, images, and subframes to finish loading.

If you want to recalculate the height then add event handler for onresize

window.addEventListener('resize', <your-handler>);
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!