gigi
gigi

Reputation: 3936

How to set margin with jquery?

I am doing this:

var sId=id.toString();
var index=sId.substring(3);
var mrg=index*221;
var el=$(id);
el.css('margin-left',mrg+'px'); and el.css('marginLeft',mrg+'px');

el is the element i want to set the margin to (correctly retrieved) and mrg is the value of the new margin

If i do $('#test1').css('margin-left',200); or $('#test1').css('marginLeft',200); it works in both ways, it's something wrong with the way i set the property.

The correct way is var el=$('#'+id);

Upvotes: 43

Views: 116702

Answers (2)

JohnP
JohnP

Reputation: 50019

Set it with a px value. Changing the code like below should work

el.css('marginLeft', mrg + 'px');

Upvotes: 18

choise
choise

Reputation: 25244

try

el.css('margin-left',mrg+'px');

Upvotes: 81

Related Questions