2

I'm trying to have the margin-bottom of an element being altered to the height of the next() element plus 12px. I tried the following, but this doesn't work.

$("div.entry-content").css("margin-bottom", $(this).next().outerHeight() + 12 + "px");

Any idea what goes wrong?

1 Answer 1

3

Try this:

$("div.entry-content").css("margin-bottom", function(){
    return $(this).next().outerHeight() + 12 + "px"
})
4
  • @snuffn Yes, I had forgotten to write the return.
    – Ram
    Aug 25, 2012 at 15:00
  • Works like a charm, thanks! Do you mind explaining why a function is needed? Aug 25, 2012 at 15:04
  • @BramVanroy Because this in your code doesn't refer to your desired element, this in the context of the function refers to the selected element, you can read about that in api.jquery.com/css/#css2
    – Ram
    Aug 25, 2012 at 15:07
  • Thanks a lot! Will read it through when I have sufficient time. I bumped into a similar problem. If you have time, please look into it: stackoverflow.com/questions/12123267/… Aug 25, 2012 at 15:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.