I am trying to resize somediv when the window is manually resized.
<script>
(function resizeElement() {
let height = window.innerHeight - 90;
somediv.style.height = height + 'px';
$( "#target" ).html( "<div>" + $( window ).width() + " , " \
+ $( window ).height() + "</div>" );
})();
// Listen for resize changes
window.addEventListener("resize", resizeElement);
</script>
This does not apply the function when the screen is resized. The following works but forces me to declare the function twice.
<script>
(function resizeElement() {
let height = window.innerHeight - 90;
somediv.style.height = height + 'px';
$( "#target" ).html( "<div>" + $( window ).width() + " , " \
+ $( window ).height() + "</div>" );
})();
// Listen for resize changes
window.addEventListener("resize", function() {
let height = window.innerHeight-90;
somediv.style.height = height+'px';
$( "#target" ).html( "<div>" + $( window ).width() + " , " + $( window ).height() + "</div>" );
}, false);
</script>
Any idea how I can avoid double declaring?
EDIT: How would it work if I wanted to make 90 a variable called somevar. How would I pass this?
EDIT SOLVED:
<script>
let padding_text = 90;
function resizeElement(padding_text) {
let height = window.innerHeight - padding_text;
somediv.style.height = height + 'px';
$( "#target" ).html( "<div><i>w:" + $( window ).width() + " h:" + $( window ).height() + "</i></div>" );
};
// Listen for resize changes
window.addEventListener("resize", function() {
resizeElement(padding_text);
}, false);
// Lets invoke it for the first time when the page loads
resizeElement(padding_text);
</script>
resizeElementliterally does not exist at the moment you calladdEventListener.