# Next and Previous button going to a different database

**URL:** <https://forum.wixstudio.com/t/next-and-previous-button-going-to-a-different-database/30985>\
**Category:** Ask the community\
**Tags:** question, code\
**Created:** [October 18, 2020, 1:54am UTC](https://forum.wixstudio.com/t/next-and-previous-button-going-to-a-different-database/30985 "2020-10-18T01:54:56Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![rhitservicesllc](https://avatars.discourse-cdn.com/v4/letter/r/3da27b/32.png) [@rhitservicesllc](https://forum.wixstudio.com/u/rhitservicesllc)\
**Post date:** [October 18, 2020, 1:54am UTC](https://forum.wixstudio.com/t/next-and-previous-button-going-to-a-different-database/30985/1 "2020-10-18T01:54:56Z")

</div>

Hello.

I have my Next and Previou s buttons working with code, but I have noticed that when I create a new database and I try to use the same code to have my buttons going to the next item it will take me to a different database.

There is anyway to keep it from doing that? I just want to see the Next and previous items in the same collection.

I have 4 dynamic pages, the first 2 I created a few months ago are working fine. but the 2 I created recently I am having the problem.

The code I am using I made it following this tutorial: [Velo Tutorial: Creating Previous and Next Buttons for a Dynamic Item Page with Code | Help Center | Wix.com](https://support.wix.com/en/article/corvid-tutorial-creating-previous-and-next-buttons-for-a-dynamic-item-page-with-code)

PS. “Anterior” = “Previous” and “Siguiente” = “Next”  
Code in Item page:

```javascript
import {local} from 'wix-storage';
import wixLocation from 'wix-location';

$w.onReady(function () {
  $w("#Anterior").disable();
  $w("#Siguiente").disable();
 
 if (local.getItem('dynamicPageURLs')) {
 const dynamicPageURLs = local.getItem('dynamicPageURLs').split(',');

 const currentPage = '/' + wixLocation.prefix + '/' + wixLocation.path.join('/');
 const currentPageIndex = dynamicPageURLs.indexOf(currentPage);

 if (currentPageIndex > 0) {
      $w("#Anterior").link = dynamicPageURLs[currentPageIndex - 1];
      $w("#Anterior").enable();
    }

 if (currentPageIndex < dynamicPageURLs.length - 1) {
      $w("#Siguiente").link = dynamicPageURLs[currentPageIndex + 1];
      $w("#Siguiente").enable();
    }
  }

```

Code in All page:

```javascript
import {local} from 'wix-storage';
import wixLocation from 'wix-location';

$w.onReady(function () {
  $w("#Anterior").disable();
  $w("#Siguiente").disable();
 
 if (local.getItem('dynamicPageURLs')) {
 const dynamicPageURLs = local.getItem('dynamicPageURLs').split(',');

 const currentPage = '/' + wixLocation.prefix + '/' + wixLocation.path.join('/');
 const currentPageIndex = dynamicPageURLs.indexOf(currentPage);

 if (currentPageIndex > 0) {
      $w("#Anterior").link = dynamicPageURLs[currentPageIndex - 1];
      $w("#Anterior").enable();
    }

 if (currentPageIndex < dynamicPageURLs.length - 1) {
      $w("#Siguiente").link = dynamicPageURLs[currentPageIndex + 1];
      $w("#Siguiente").enable();
    }
  }
} );

```

---

<div class="post-metadata">

**Author:** ![yisrael-wix](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/yisrael-wix/32/1107_2.png) [@yisrael-wix](https://forum.wixstudio.com/u/yisrael-wix)\
**Post date:** [October 18, 2020, 9:31am UTC](https://forum.wixstudio.com/t/next-and-previous-button-going-to-a-different-database/30985/2 "2020-10-18T09:31:05Z")

</div>

If you are trying to do this for another collection, then you will have to save the URLs associated with that collection using another storage key. For example, something like this:

For the first collection:

```javascript
local.getItem('dynamicPageURLs')

```

For the second collection:

```javascript
local.getItem('dynamicPageURLs2')

```

Or, maybe I just don’t properly understand your problem.

---

<div class="post-metadata">

**Author:** ![rhitservicesllc](https://avatars.discourse-cdn.com/v4/letter/r/3da27b/32.png) [@rhitservicesllc](https://forum.wixstudio.com/u/rhitservicesllc)\
**Post date:** [October 18, 2020, 12:52pm UTC](https://forum.wixstudio.com/t/next-and-previous-button-going-to-a-different-database/30985/3 "2020-10-18T12:52:19Z")

</div>

I tried doing that and then it jus did not work, next and previous button were disable like it could not find any other item

i will try again I guess.

---

<div class="post-metadata">

**Author:** ![yisrael-wix](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/yisrael-wix/32/1107_2.png) [@yisrael-wix](https://forum.wixstudio.com/u/yisrael-wix)\
**Post date:** [October 18, 2020, 1:08pm UTC](https://forum.wixstudio.com/t/next-and-previous-button-going-to-a-different-database/30985/4 "2020-10-18T13:08:51Z")

</div>

@rhitservicesllc Keep in mind that if you’re using two separate collections, you will need to separate the code accordingly.

---

<div class="post-metadata">

**Author:** ![yisrael-wix](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/yisrael-wix/32/1107_2.png) [@yisrael-wix](https://forum.wixstudio.com/u/yisrael-wix)\
**Post date:** [October 18, 2020, 1:13pm UTC](https://forum.wixstudio.com/t/next-and-previous-button-going-to-a-different-database/30985/5 "2020-10-18T13:13:34Z")

</div>

You might find the [Dynamic Page Loop](https://www.grampsworkbench.com/Examples/Dynamic-Page-Loop) example useful. It will show you how to handle **previous** and **next** without using local storage. And it even throws in a way to loop at the end from the last to first.

---

<div class="post-metadata">

**Author:** ![rhitservicesllc](https://avatars.discourse-cdn.com/v4/letter/r/3da27b/32.png) [@rhitservicesllc](https://forum.wixstudio.com/u/rhitservicesllc)\
**Post date:** [October 18, 2020, 2:23pm UTC](https://forum.wixstudio.com/t/next-and-previous-button-going-to-a-different-database/30985/6 "2020-10-18T14:23:20Z")

</div>

Thanks! That kind of helps, but now i am having 2 issues with your example.

1- The Items are not switching in order, if i click Next or Prev, it will go to another item that is not the next in line.

2- See the picture below, for some reason when going to one of the items is giving me that error, is adding “/cerrar/” in the middle, but is not doing it for all the pages.

![](https://us1.discourse-cdn.com/wix/original/3X/e/8/e82fd89586b46d839fad85f7a8fa8754ae6f3a7f.png)

Any suggestinons?

And thanks again!

PS. I pretty much used the same code, but change the location from /countries/ to /ibkr-mobile/

---

<div class="post-metadata">

**Author:** ![rhitservicesllc](https://avatars.discourse-cdn.com/v4/letter/r/3da27b/32.png) [@rhitservicesllc](https://forum.wixstudio.com/u/rhitservicesllc)\
**Post date:** [October 27, 2020, 12:44pm UTC](https://forum.wixstudio.com/t/next-and-previous-button-going-to-a-different-database/30985/7 "2020-10-27T12:44:15Z")

</div>

Nothing?

---

<div class="post-metadata">

**Author:** ![yisrael-wix](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/yisrael-wix/32/1107_2.png) [@yisrael-wix](https://forum.wixstudio.com/u/yisrael-wix)\
**Post date:** [October 27, 2020, 1:19pm UTC](https://forum.wixstudio.com/t/next-and-previous-button-going-to-a-different-database/30985/8 "2020-10-27T13:19:27Z")

</div>

@rhitservicesllc My example works fine - just tried it (again). You should review that changes that you made.
