I need the width of a specific <div>
to get smaller by 50px every time a JavaScript function is called.
I tried
document.getElementById("Zivot").style.width = "100px";
But that just sets the width, and doesn't add or subtract it.
I need the width of a specific <div>
to get smaller by 50px every time a JavaScript function is called.
I tried
document.getElementById("Zivot").style.width = "100px";
But that just sets the width, and doesn't add or subtract it.
Use this code. Get the width using offsetWidth and reduce it by 50px every time.
var element = document.getElementById('Zivot');
element.style.width = (element.offsetWidth - 50) + 'px';
A pure Javascript way of getting this working-
var zivotID = document.getElementById("Zivot");
zivotID.style.width = zivotID.offsetWidth - 50;
document.getElementById("Zivot").style.width or myID.style.width - Here you actually set the value of width. I mean, you wait for a value to be given so that you can set.
document.getElementById("Zivot").offsetWidth or myID.offsetWidth - This gets the width of the div/element.
Some reference links-
How to find the width of a div using raw Javascript?
How do I retrieve an HTML element's actual width and height?
Hope it helps :)
var currentWidth = $('#Zivot').width();
$('#Zivot').css('width', currentWidth - 50 + 'px');