0

This is my code, which is not working.

html:

<div class=“footerA”> aaaa </div> <div class=“footerB”></div>

Jquery

$(‘.footerA’).on(‘resize’,function(){ $(‘.footerB’).css(‘width’, $(‘.footerA’).width()) })

I expect footerB’s width changes to when footerA’s width change. The change means when I expand/shrink the browser.

But it doesnt work. Please help.

1 Answers1

1

If you want a JS solution, see here:

You cannot bind "resize" to elements, instead use ResizeObserver

Also note that you should use some inline-type element/styling. div elements are display: block by default, hence I've added display: inline-block; to show this point.

In the snippet, I made #footerA editable (like an input) so that you can change the div's content.

const footerA = document.getElementById("footerA");
const footerB = document.getElementById("footerB");

const resizeObserverCallback = () => {
  footerB.style.width = `${footerA.getBoundingClientRect().width}px`;
}

if (footerA && footerB) {
  new ResizeObserver(resizeObserverCallback).observe(footerA)
}
.footer {
  background-color: orange;
  display: inline-block;
}
<div id="footerA" class="footer" contenteditable="true">aaaa</div>
<div id="footerB" class="footer">bb</div>

This question could be a duplicate of this question

Harrison
  • 1,654
  • 6
  • 11
  • 19