3

I've been trying to figure out the proper syntax for the pure JavaScript version of this JQuery code.

$('body').css('margin-bottom', $('footer').height());

So far what I have is:

var body = document.getElementById('#body');
var footer = document.getElementById('#footer');

body.style.marginBottom = footer.style.height;

This assignment only works if I define the footer height in JavaScript.

I'm aware of this question How to make a pure javascript .css() function using variable variables [duplicate] and the questions similar to it, but still am unable to get it correct. I know I could just use JQuery, but I don't see the value in loading a whole library to do very simple things.

Thank you in advance.

Community
  • 1
  • 1
Xavier
  • 119
  • 1
  • 13

2 Answers2

1

Open JavaScript console and type console.log(document.body.style) to see all style attributes that document.body has. Also I think it's document.body and not just body.

Body seems to have the following margin properties:

margin: ""
marginBottom: ""
marginLeft: ""
marginRight: ""
marginTop: ""

So document.body.style.marginBottom = '15px' should work for example. Remember that you must specify the unit, like px.

Swiffy
  • 4,401
  • 2
  • 23
  • 49
0

Use offsetHeight:

body.style.marginBottom = footer.offsetHeight;
hillai
  • 93
  • 7