Adi
Adi

Reputation: 4022

jquery syntax for making positive variable integer negative

I am new to js and a bit stuck (with something that is probably very ease to fix)

I have worked out the height of a div, but I want the variable to be a negative value (so i can centre something horizontally)

Here is the wrong jquery

var modal_height = $('#modal div').outerHeight()/2;
$('#modal div').css('marginTop','-'+modal_height);

Basically as you can see im trying to make the final marginTop value a negative one (i.e. adding a minus sign before the variable)

What is the correct syntax?

Upvotes: 0

Views: 3029

Answers (1)

SLaks
SLaks

Reputation: 887275

You need to use the unary minus operator: -modal_height.
This operator returns a negative number.
(If the original number is already negative, it will return a positive number instead)

Upvotes: 5

Related Questions