3

Say I want to set horizontal padding of a styled element to variable 'x' and set vertical to a variable 'y'. Of course, this is possible:

var docex = document.getElementById.style;
docex.paddingTop = y;
docex.paddingBottom = y;
docex.paddingLeft = x;
docex.paddingRight = x;

Is there a shorter way for me to do this, such as something akin to:

var docex = document.getElementById.style;
docex.paddingTop = docex.paddingBottom = y;
docex.paddingLeft = docex.paddingRight = x;

However, as I understand it, the above would not work and would declare a global variable, as I saw from another question on this site. So, what would be the most efficient minimal way for me to implement an idea similar to above, that is possible with vanilla javascript?

Thanks in advance!

4
  • Just introduce a function which accepts x,y and do the stuff inside the function Commented Jan 21, 2017 at 12:55
  • What global variable? Commented Jan 21, 2017 at 12:57
  • 2
    you can do some css trickery for that specific case -> document.getElementById('#id').style.cssText='padding: '+x+' '+y; Commented Jan 21, 2017 at 12:57
  • won't this override the current inline-style? Commented Jan 21, 2017 at 13:06

3 Answers 3

2

You can use the shorthand CSS rule for that:

docex.padding = y + " " + x;

This sets y as top and bottom padding and x as left and right padding.

MDN Reference

Sign up to request clarification or add additional context in comments.

1 Comment

Ah, yeah. This wasn't working for me earlier because I was writing it as docex.padding = y + x; without the space in quotation. And I didn't even know writing just two variables means that it assumes horizontal and vertical. Thanks a lot!
1

However, as I understand it, the above would not work and would declare a global variable

It wouldn't.

Compare these two examples:


var foo = bar = 2;

This:

  1. Declares foo as a local variable
  2. Assigns 2 to bar implicitly creating bar as a global while it does it
  3. Assigns bar to foo

obj.foo = obj.bar = 2;

This:

  1. Assigns 2 to obj.bar implicitly creating bar as a property of obj if it doesn't already exist
  2. Assigns obj.bar to obj.foo implicitly creating foo as a property of obj if it doesn't already exist

It doesn't create a global because you don't have any code which mentions a variable name.


That said, since you are changing all the padding properties, you can set the padding shorthand property instead of doing each of the four individually.

padding: <top> <right> <bottom> <left>

or

padding: <vertical> <horizontal>

Thus:

docex.padding = y + " " + x;

Comments

0

You can do it using style.padding

var x="50px";
var y="25px";

var ele = document.getElementById('o');

ele.style.padding = y+' '+x;
#o{
  width:200px;
  height:100px;
  border:1px solid black;
  box-sizing:border-box;
}
#i{
  width:100px;
  height:50px;
  background:red;
}
<div id="o">
<div id="i">
  </div></div>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.