Reputation: 24748
I try to get the eventlistener for resize to trigger but it does nothing.
Why does nothing happends and how can I fix it?
/* Load event directly - FAIL */
document.querySelector("div").addEventListener("resize", () => {
console.log("resize");
});
/* Wait until dom loads first - FAIL */
window.addEventListener("DOMContentLoaded", () => {
document.querySelector("div").addEventListener("resize", () => {
console.log("resize");
});
});
div {
width: 300px;
height: 100px;
background: #eee;
resize: both;
overflow: auto;
}
<div></div>
Upvotes: 0
Views: 85
Reputation: 654
look at Resize_Observer_API docs
and there is my tiny prototype of it, like in your case:
html:
<div style="border: 1px solid #444;" id="textarea" contenteditable=""> some text</div>
<div id="result"></div>
js:
function outputsize() {
document.getElementById('result').innerHTML=`
${textarea.offsetWidth} ${textarea.offsetHeight}
`
}
outputsize()
new ResizeObserver(outputsize).observe(textarea)
https://codepen.io/zaslavskij/pen/ZELZMYK
Upvotes: 1