Reputation: 621
I'm trying to set a margin-left using a variable in JavaScript. Is this possible? How can this be done?
The margin left needs to be the clientWidth of the text:
var hoverBubble = document.getElementById(id);
var linkWidth = link.clientWidth + 1;
hoverBubble.style.margin-left = linkWidth + 'px';
Upvotes: 0
Views: 505
Reputation: 70189
It is possible, you just have to camelCase style properties.
hoverBubble.style.marginLeft = linkWidth + 'px';
You just can't use an hyphen (minus sign) as in the question.
Otherwise the interpreter will "think" that you have a subtraction operation in the left-side of the assignment, which results in a syntax error.
You can see that error on proper debugging tools, such as Chrome Dev Tools (F12 in Chrome and select console) or Firebug (Install Firebug and press F12 on Firefox).
Upvotes: 4