0

I want to increase the right margin of .questionarea by 400px every time a user clicks on .go. Here's my Javascript.

            var marright = "-400px"
        $(".go").click(function(){
          $(".questionarea").animate({
            marginRight: marright
          }, 300 );
         marright += "-400px";
        });

If you take out the second last line, the button works once but I've got no idea of the syntax for increasing the size of the margin on each additional click.

1
  • 1
    You are appending "-400px" each time. So marright is becoming something like "-400px-400px-400px-400px" (A string). Add the "px" after the arithmetic. Commented Jan 16, 2013 at 14:49

2 Answers 2

2

Try this:

var marright = -400;
$(".go").click(function(){
    $(".questionarea").animate({
        marginRight: marright
    }, 300, function(){
        marright += -400;
    });
});

You don't need to have marright as a string with the px on the end of it. Jquery is clever enough to work that out for you.

Sign up to request clarification or add additional context in comments.

Comments

0
    $(".go").click(function(){
      $(".questionarea").animate({
        marginRight: "-=400"
      }, 300 );
    });

I think it should be like this.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.