5

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.

Brett Wolfington
  • 6,587
  • 4
  • 32
  • 51
Ivan
  • 816
  • 2
  • 9
  • 14

3 Answers3

7

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'; 
Sumit
  • 1,619
  • 1
  • 11
  • 24
2

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?

HTMLElement.offsetWidth

How do I retrieve an HTML element's actual width and height?

Hope it helps :)

Community
  • 1
  • 1
bozzmob
  • 12,364
  • 16
  • 50
  • 73
0
var currentWidth = $('#Zivot').width();
$('#Zivot').css('width', currentWidth - 50 + 'px');
Donnie D'Amato
  • 3,832
  • 1
  • 15
  • 40