javascript changing css properties

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
kirilisa
Forum Commoner
Posts: 28
Joined: Fri Dec 05, 2003 4:41 pm

javascript changing css properties

Post by kirilisa »

Can anyone tell me how to change the top and left css properties with javascript?

I know you can do for instance

Code: Select all

var div1 = document.getElementById('box1');
div1.style.display = 'none';
but what if I want to do

Code: Select all

div1.style.left= 35mm;
?? It doesnt' work.

Thanks.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

you must use strings.
kirilisa
Forum Commoner
Posts: 28
Joined: Fri Dec 05, 2003 4:41 pm

Post by kirilisa »

Sorry, I don't know what you mean. Could you give an example, or be more specific?

Thanks.
ryanlwh
Forum Commoner
Posts: 84
Joined: Wed Sep 14, 2005 1:29 pm

Post by ryanlwh »

Code: Select all

div1.style.left= '35mm';
kirilisa
Forum Commoner
Posts: 28
Joined: Fri Dec 05, 2003 4:41 pm

Post by kirilisa »

Oh I see what you were saying. I don't think that is my probelm though. I think, from looking at it again, my problem is not knowing how to do math properly with JS.



In my case, I have

Code: Select all

var =  3 + 7;
div1.style.left=var;
(The reason that

Code: Select all

var = 3 + 7
is because it is adding up a bunch of defined or otherwise gotten constants from PHP.)

I think the issue is that it isn't summing the 3+7 when it changes the CSS. It's interesting: if I

Code: Select all

alert(var)
it gives me 10, but I bet when setting the CSS it is treating it like a string, or something. is there some way to type the results? to force it to actually add them up?

Bleh. I hate Javascript.
ryanlwh
Forum Commoner
Posts: 84
Joined: Wed Sep 14, 2005 1:29 pm

Post by ryanlwh »

var is a keyword to declare a variable... maybe

Code: Select all

var leftSide = 3+7;
div1.style.left = leftSide;
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

giving CSS a number when it needs a length value will give basically undefined results.

Code: Select all

var x = 3 + 7;
obj.style.left = x + 'mm';
will set '10mm'
Post Reply