0

Hopefully this is a quick easy one for you.

Here is my code:

var p7slide = 0;
$("#p7-forward").click (function () {
    p7animate = null;
    if (p7slide == 6)   {
        $("#p7-6").fadeOut("slow", function ()  {
            p7slide = 0;            
        });
    }
    else    {
        p7slide++;
        $("#p7-" + p7slide).fadeIn("slow");
        <!--$("#p7-" + "p7slide - 1").fadeOut("slow");-->
    };
});

The part i refer to is the commented out line in the else statement.

What i need this to do is to find p7slide and take 1 from this value. Then use that as the selector. E.g. If p7slide = 2 the selector would put together #p7-1. For some reason this statement isn't working.

Any ideas what Ive done wrong here?

Thanks

1
  • 1
    Note that HTML comments are invalid in JavaScript. Commented Dec 14, 2011 at 12:18

2 Answers 2

2

You've hard coded it as a string rather than an operation.

replace

$("#p7-" + "p7slide - 1").fadeOut("slow");

with

var slideNumber = p7slide - 1;
$("#p7-" + slideNumber).fadeOut("slow");

Note: In JS, if you use + operator with a string first, then a number, then you'll concatenate the number as a string to the end of the first string, see: http://www.quirksmode.org/js/strings.html for more details.

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

Comments

0

Try removing the " " tags as you cannot use numeric functions on a string

 $("#p7-" + p7slide - 1).fadeOut("slow");

1 Comment

"you cannot use numeric functions on a string" isn't accurate. + isn't a function, it's an operator. You can use operator with string, but if the first operand is a string, it will concatenate then number as a string to the first, see: quirksmode.org/js/strings.html for more details.

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.