2

I don't know why it can't find the height. Any help would be appreciated.

var h = document.getElementById('big_button').clientHeight,
    center = -h / 2;
window.onload = function () {
    document.getElementById('big_button_container2').style.marginTop = 'center';
};

http://jsfiddle.net/4zux6/

dillonbrannick
  • 322
  • 1
  • 5
  • 16

1 Answers1

2

You can use document.getElementsByClass instead as you have only elements with class names not IDs.

i.e.

var h = document.getElementsByClassName('big_button')[0].clientHeight,
    center = -h / 2;
window.onload = function () {
    document.getElementsByClassName('big_button_container2')[0].style.marginTop = 'center';
};
Andrew Robinson
  • 346
  • 4
  • 11
  • You need to access individual elements from the returned collection. I don't know which one is meant to be targeted, but the first would be at index `0`. – cookie monster Jan 19 '14 at 01:35
  • 1
    Now I'm getting this _Uncaught TypeError: Cannot set property 'marginTop' of undefined_. How is center undefined? – dillonbrannick Jan 19 '14 at 01:49
  • 2
    @dillonbrannick I forgot that that give you an array of elements with that class name. So you have to access the element you want at index 0. I changed the answer to reflect that. Does at work? Alternatively just add IDs to the elements you want to access and use your previous method of getElementById. – Andrew Robinson Jan 19 '14 at 03:18
  • Note that if you have more than one element with the same class name you would have to iterate through them like in this [answer](http://stackoverflow.com/a/14142710/3208425). – Andrew Robinson Jan 19 '14 at 04:03