[javascript] 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);

This question is related to javascript jquery

The answer is


try

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

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

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