0

I don't see anything wrong with this code, but it's not working as expected.

function slide(slideIndex, slideDirection) {
    console.log(slideDirection); // outputs 'right'
    $('.slide').animate({slideDirection: '-=940'}, 400);
}    

$(function(){
    $('.prev','.slide').click(function (e) {
        e.preventDefault();

        var slideIndex = $(this).closest('.slide').index(),
            slideDirection = 'right';

    slide(slideIndex, slideDirection);
    });
});

If I use just the string 'right' in the animate method, it works. What am I doing wrong?

1
  • As I mentioned, using the string 'right' directly in the animate function works just fine. Using that variable in the animate method doesn't seem to work. It is being ignored. Commented Sep 6, 2012 at 8:14

1 Answer 1

5

You're creating an object literal with the property called slideDirection, you're not using the value of the argument. To do that, you'll need to create an object in 2 steps, separately:

var obj ={};//empty object literal
obj[slideDirection] = '-=940';//assign a new property
$('.slide').animate(obj, 400);

That should do the trick. Your object, in json format looks like this: {"slideDirection":"-=940"} whereas mine (or the object created as I explained) looks like this: {"right":"-=940"}. The latter is what you need, if I'm not mistaken

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

1 Comment

Nitpick: "To do that, you'll need to create your object literal in 2 steps" -- if you do so, it's no longer a literal.

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.