Jon Snow's papa Jon Snow's papa - 4 months ago 12
jQuery Question

Add to current CSS top value

I have this jQuery:

$(".ta").siblings(".Aname:eq(0)").css("top",+=$(".Text").height()+15);


But it is not working. I have tried this way too:

$(".ta").siblings(".Aname:eq(0)").css("top",'+=$(".Text").height()+15');


But it also did not work.

Answer

You have to grab the current top value, and then add a new value, and then append the "px" back on when you set the value using .css()

Here is what it would look like:

var element = $(".ta").siblings(".Aname:eq(0)");
var currTop = Number(element.css("top").replace("px", ""));
var amountToAdd = $(".Text").height() + 15;
$(element).css("top", (currTop + amountToAdd + "px"));

And here is a WORKING DEMO