0

I have one array like this.

var myArray = ["1", "2", "3", "4", "5"];

I need to find the next element when I click next. Suppose I've 2 in a variable, when I click "next" button, I need to check the array and get 3 and so on.

5
  • What have you tried till now? Do you have any partially/non-working script? Commented May 27, 2014 at 5:32
  • How does your html look like? Commented May 27, 2014 at 5:32
  • what you think about loop?? Commented May 27, 2014 at 5:32
  • what's the problem with myArray[var] ? Commented May 27, 2014 at 5:33
  • There's no need for JQuery. Use plain JS. Commented May 27, 2014 at 5:33

3 Answers 3

4

jQuery:

You should use .inArray() with modulus operator:

var myArray = ["1", "2", "3", "4", "5"];
var val = "2";
var next = myArray[($.inArray(val, myArray) + 1) % myArray.length];

DEMO

JavaScript:

var myArray = ["1", "2", "3", "4", "5"];
var val = "2";
var index = myArray.indexOf(val);
if(index >= 0 && index < myArray.length - 1){
   var next = myArray[index + 1];
   alert(next);
}

DEMO

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

6 Comments

Thanks.. But, when I give 5, it should not show anything.. Because thats the end of the array.. Could you please check it?
Why have you used modulus in the first place?
When I print using $.each(mySqllist, function(index, value){ console.log("Index::"+index+" Value is:"+value); }); It gives me the list of values inside it.. But, when I use your code, it gives me -1 as index value
@RupamDatta: If you try to get next element of your last element in array. It will show undefined if you use modulus it will go back to first element. Hope this helps you :)
Its working fine check both jsfiddle.net/9CGqT/2 and jsfiddle.net/kunknown/9CGqT/3
|
0
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

<input type="button" value="next" id="next">

<script>
    var globle = 0; // by default first page
    var myArray = ["1", "2", "3", "4", "5"];
    $(document).ready(function(){
        $("#next").click(function(){
            if(globle == myArray.length){
                 globle = 0;
            }

            alert(myArray[globle]);
            globle++;
        });
    });
</script>

Comments

0

Try this:

var myArray = ["1", "2", "3", "4", "5"];
var val = "2";
function checknext() {
       var next = $.inArray(val, myArray) + 1;
       if (next < myArray.length) {
            val = myArray[next];
            alert(val);
       }
       else {
           alert("No More Item Exist");
       }
  }

  function checkprv() {                
       var prv = $.inArray(val, myArray) - 1;
         if (prv >= 0) {
           val = myArray[prv];
           alert(val);
         }
         else {
           alert("No More Item Exist");
         }

Working Example

1 Comment

Thanks Ishan for your answer

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.