0

What is the shorthand to assign a CSS style and add the pixels on the same line? The following doesn't work though it should be exceptionally clear what I'm trying to do:

header.style.top = (h + 100)+px;
scniro
  • 16,844
  • 8
  • 62
  • 106
John
  • 1
  • 13
  • 98
  • 177

3 Answers3

6

px is not defined. I believe you are intending to concat a string. Observe the following...

header.style.top = (h + 100) + 'px';

JSFiddle Link - simplified demo

scniro
  • 16,844
  • 8
  • 62
  • 106
  • It was late Friday evening and the left brain doesn't function well after hours of coding. It was a two-part mistake on my end; yeah there was no `px` object, I forgot to put quotes to make it a string. I also made another mistake though unrelated to the code here. Thanks! – John Feb 22 '16 at 17:21
4
header.style.top = (h + 100) + 'px';
satish
  • 571
  • 3
  • 6
0

I believe you don't need to specify the px value. It defaults to pixels

checkout this question: Using Javascript to add width to a HTML element

As found here http://javascript.info/tutorial/styles-and-classes-getcomputedstyle they say to enclose the px in brackets 'px'. Like the above answers.

Community
  • 1
  • 1
James111
  • 15,378
  • 15
  • 78
  • 121