wowzuzz
wowzuzz

Reputation: 1388

Incrementing to next id in javascript

I have an image I click which brings me to a page with some info and two buttons which are previous and next.

My code keeps going back to the same slide. How would I increment my "id" attribute to add +1 on my id attribute every time I click the next button or -1 for previous?

Here is my code

    $(".portfolioLink").click(function(){

        // Hide slide and portfolio piece info
        $(".inner-content.portfolio").show();

        // Reset slides timer and pagination
        clearInterval($('#slides').data('interval'));
        $("ul.pagination li").removeClass("current");

        var slideID = $(this).parent().next().attr("id");

        $("#next").click(function() {
            slideswitch(slideID);
        });
    }

Upvotes: 0

Views: 649

Answers (2)

Matt Burland
Matt Burland

Reputation: 45155

Use a regex to separate the base part of your id from the numerical part. Then parse the numerical part as an int and increment or decrement it as needed. Then concatenation it back on the the base part of your id.

Upvotes: 0

Kuf
Kuf

Reputation: 17846

try

var slideID = $(this).parent().next().attr("id");
slideID = 1 + parseInt(slideID);

if the ID has some text to it (id = "div1" for example) you can write the id number:

var slideID = $(this).attr("id").match(/\d+/);

Upvotes: 1

Related Questions