Philip
Philip

Reputation: 621

passing a variable into style css for javascript

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

Answers (2)

Fabrício Matté
Fabrício Matté

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

Sushanth --
Sushanth --

Reputation: 55750

Try this

hoverBubble.style.marginLeft = linkWidth + 'px';

Upvotes: 0

Related Questions