Reputation: 69
How I set the 'top' css value as a variable using $(this) ?
Thus far I have not been able to do so with this code:
$("#thumb").click(function () {
var TopV = $(this).css('top');
alert(TopV)
});
Upvotes: 0
Views: 2064
Reputation: 43168
What do you want to set it to? Something like this will do it:
$("#thumb").click(function () {
var targetTop = '100px';
$(this).css('top', targetTop);
var TopV = $(this).css('top');
alert(TopV)
});
Upvotes: 1
Reputation: 1186
var offset = $(this).offset();
$(this).html( "top: " + offset.top );
you can try position() also
Upvotes: 1
Reputation: 117353
That code snippet is attempting to read the CSS 'top' property, not write to it. It probably does not exist on most elements.
Did you mean that you wanted to set it to some value, like this?
$(this).css('top', '5px');
You should also be aware that the 'top' property is only meaningful if the element it is set on doesn't have 'static' positioning - that is, if it has absolute, relative or fixed. Otherwise it's ignored: it won't do anything, and probably won't be readable.
More info on the top CSS property
Upvotes: 4