Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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);

like image 262
gigi Avatar asked Mar 15 '11 16:03

gigi


People also ask

How to set margin using jQuery?

log("margin="+margin+" padding="+padding); } var elem = $("div. header"); //set the element to inspect logMarginPadding(elem); //sets elem. css("margin","10px"); elem. css("padding","10px"); logMarginPadding(elem); })(jQuery);

How to add margin left in jQuery?

css() Incrementing margin-left property Increments the margin-left property. Example: css("margin-left", "+=50px") - Move the mouse over the <h1> elements to increment the margin-left property. $(document). ready(function() { $("h1").


1 Answers

try

el.css('margin-left',mrg+'px'); 
like image 62
choise Avatar answered Sep 23 '22 18:09

choise