Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

change margin-left with JS

I want a dynamical margin-left on a given div.

I tried this code :

window.onload = function()
{
    var pageWidth = window.innerWidth; 
    var size = (pageWidth - 200 ) / 2;
    $('#search').css('margin-Left', size));
    alert(size);
 };

which doesn't work (the alert is here just for test purpose), the problem is coming from the ligne $('#search').css('margin-Left', size)); but I can't find out where...

I've tried with a lowercase l on margin-left and didn't work.

like image 494
Thomas Ayoub Avatar asked Oct 17 '13 19:10

Thomas Ayoub


2 Answers

Would the following work for you?

document.getElementById("search").style.marginLeft = size + "px";
like image 94
Arthur Weborg Avatar answered Sep 21 '22 23:09

Arthur Weborg


You need to specify the type of unit you want to use.

$('#search').css('margin-left', size + 'px');
like image 40
Giovanni Silveira Avatar answered Sep 21 '22 23:09

Giovanni Silveira