<p>The way you want to achieve this won't work. Twig is just a render engine for php so you can't execute it during your runtime. You could load <strong>all</strong> entries during the page load and create a twig filter to <code>json_decode</code> them in order to gain access with javascript or you can do something really really nasty like </p>

<pre><code>{% set allEntries = craft.entries.section('comics').order('id asc') %}
&lt;script&gt;
    var entries = [];
    {% for entry in allEntries %}
        entries.push({
            title: "{{ entry.title }}",
            body: "{{ entry.body }}",
            url: "{{ entry.getUrl() }}"
            // ..... 
        });
    {% endfor %}
&lt;/script&gt;
</code></pre>

<p>To get an array of objects in javascript. But I would not suggest this. The clean way would be to create an ajax request and fetch the entry with php</p>

<p>Have a look at <a href="https://craftcms.stackexchange.com/a/23001/5557">this</a> to learn how to create an ajax request to a controller. You would have to change your controller to:</p>

<pre><code>public function actionSearchEntry()
{
    $id = craft()-&gt;request-&gt;getParam('id');
    $element = craft()-&gt;elements-&gt;getElementById($id);

    $criteria = craft()-&gt;elements-&gt;getCriteria(ElementType::Entry);
    $criteria-&gt;section = "comics";
    $criteria-&gt;orderBy = "id asc";
    if(!$nextElement = $element-&gt;getNext($criteria)){
        $this-&gt;returnJson([
            'success'   =&gt; false
        ]);
    }

    $this-&gt;returnJson([
        'element'      =&gt; [
            'title' =&gt; $nextElement-&gt;title,
            'body'  =&gt; $nextElement-&gt;body,
            // ....
            'id'    =&gt; $nextElement-&gt;id
        ],
        'success'   =&gt; true
    ]);
}
</code></pre>

<p>In your javascript you'll create an ajax request and change the values to the one you received if it was successful (if not there will be probably no other entries.</p>

CC BY-SA 3.0
2018-01-08T16:48:13.280Z
2018-01-08T16:48:13.280Z
Robin Schambach
<p>I want to iterate through my entries on the click of a 'next' and 'previous' button.</p>

<p>I have started off using getNext() and getPrev() by I'm in a bit of a rut.</p>

<pre><code>{% set entry = craft.entries.section('comics').first() %}
{% set asset = entry.comic.first() %}
{% set params = craft.entries.section('comics').order('id asc') %}
{% set prevEntry = entry.getPrev(params) %}
{% set prevAsset = prevEntry.comic.first() %}
{% set nextEntry = entry.getNext(params) %}
{% set nextAsset = nextEntry.comic.first() %}
</code></pre>

<p>This is the Twig code. To display the prevEntry I have been using jQuery. Here is that code.</p>

<pre><code>    function prevComic() {
      var $prevTitle = "{{ prevEntry.title }}";
      $(".comic-title").text($prevTitle);
      $(".comic").attr('src', '{{ prevAsset.url }}');
      $(".comic-caption").text("{{ prevEntry.body }}");
    };
    function nextComic() {
      var $nextTitle = "{{ nextEntry.title }}";
      $(".comic-title").text($nextTitle);
      $(".comic").attr('src', '{{ nextAsset.url }}');
      $(".comic-caption").text("{{ nextEntry.body }}");
    };
</code></pre>

<p>Then I'm using onclick() on the buttons to call the functions. 
All I really want is for the 'previous' button to give me the previous entry to the entry currently displayed, and for the 'next' button to give me the next entry to the entry currently displayed. 
Can anyone help me?
Thank you :) </p>

CC BY-SA 3.0
2018-01-06T16:00:20.347Z
Do you want to reload the entries via page reload or ajax request?
Robin Schambach
CC BY-SA 3.0
2018-01-06T16:22:17.393Z
I don't mind, I just want it to work.
timbutcher
CC BY-SA 3.0
2018-01-06T16:23:37.510Z
If that's the case, you could just use this answer https://craftcms.stackexchange.com/a/23004/5557 and don't use javascript and instead reload the page when someone clicks on the button
Robin Schambach
CC BY-SA 3.0
2018-01-08T16:14:23.623Z
How do I do it without Javascript? Is there a way to trigger things onclick within CraftCMS?
timbutcher
CC BY-SA 3.0
2018-01-08T16:16:45.617Z
With a page reload. You create a link to a new page with different parameters to display different entries. That's why I asked if the request type matters. The other way would be a much smoother, cleaner ajax request to reload the things. But most people are too lazy to do it. Since you said you don't mind I tough you are the same
Robin Schambach
CC BY-SA 3.0
2018-01-08T16:22:10.320Z
If you've got 10 minutes, would you mind helping me with this over Skype? I'll PayPal you some cash after we're done :)
timbutcher
CC BY-SA 3.0
2018-01-08T16:23:07.497Z
I don't have skype we can use the chat here https://chat.stackexchange.com/rooms/70444/plugin-developement
Robin Schambach
<templating><elementcriteriamodel>
How can I iterate through my entries?
# How can I iterate through my entries?

(Asked by: username_0 on 2018-01-06)

I want to iterate through my entries on the click of a 'next' and 'previous' button.
I have started off using getNext() and getPrev() by I'm in a bit of a rut.
{% set entry = craft.entries.section('comics').first() %}
{% set asset = entry.comic.first() %}
{% set params = craft.entries.section('comics').order('id asc') %}
{% set prevEntry = entry.getPrev(params) %}
{% set prevAsset = prevEntry.comic.first() %}
{% set nextEntry = entry.getNext(params) %}
{% set nextAsset = nextEntry.comic.first() %}

This is the Twig code. To display the prevEntry I have been using jQuery. Here is that code.
    function prevComic() {
      var $prevTitle = "{{ prevEntry.title }}";
      $(".comic-title").text($prevTitle);
      $(".comic").attr('src', '{{ prevAsset.url }}');
      $(".comic-caption").text("{{ prevEntry.body }}");
    };
    function nextComic() {
      var $nextTitle = "{{ nextEntry.title }}";
      $(".comic-title").text($nextTitle);
      $(".comic").attr('src', '{{ nextAsset.url }}');
      $(".comic-caption").text("{{ nextEntry.body }}");
    };

Then I'm using onclick() on the buttons to call the functions. 
All I really want is for the 'previous' button to give me the previous entry to the entry currently displayed, and for the 'next' button to give me the next entry to the entry currently displayed. 
Can anyone help me?
Thank you :)

**Tags:** templating, elementcriteriamodel
> **Comments:**
> **username_1**: Do you want to reload the entries via page reload or ajax request?  
> **username_0**: I don't mind, I just want it to work.  
> **username_1**: If that's the case, you could just use this answer https://craftcms.stackexchange.com/a/23004/5557 and don't use javascript and instead reload the page when someone clicks on the button  
> **username_0**: How do I do it without Javascript? Is there a way to trigger things onclick within CraftCMS?  
> **username_1**: With a page reload. You create a link to a new page with different parameters to display different entries. That's why I asked if the request type matters. The other way would be a much smoother, cleaner ajax request to reload the things. But most people are too lazy to do it. Since you said you don't mind I tough you are the same  
> **username_0**: If you've got 10 minutes, would you mind helping me with this over Skype? I'll PayPal you some cash after we're done :)  
> **username_1**: I don't have skype we can use the chat here https://chat.stackexchange.com/rooms/70444/plugin-developement  

---

## 1 answers

### Answer by username_1 on 2018-01-08. Score: 2 | Accepted answer

The way you want to achieve this won't work. Twig is just a render engine for php so you can't execute it during your runtime. You could load all entries during the page load and create a twig filter to json_decode them in order to gain access with javascript or you can do something really really nasty like 
{% set allEntries = craft.entries.section('comics').order('id asc') %}


To get an array of objects in javascript. But I would not suggest this. The clean way would be to create an ajax request and fetch the entry with php
Have a look at this to learn how to create an ajax request to a controller. You would have to change your controller to:
public function actionSearchEntry()
{
    $id = craft()->request->getParam('id');
    $element = craft()->elements->getElementById($id);

    $criteria = craft()->elements->getCriteria(ElementType::Entry);
    $criteria->section = "comics";
    $criteria->orderBy = "id asc";
    if(!$nextElement = $element->getNext($criteria)){
        $this->returnJson([
            'success'   => false
        ]);
    }

    $this->returnJson([
        'element'      => [
            'title' => $nextElement->title,
            'body'  => $nextElement->body,
            // ....
            'id'    => $nextElement->id
        ],
        'success'   => true
    ]);
}

In your javascript you'll create an ajax request and change the values to the one you received if it was successful (if not there will be probably no other entries.


---

