<p>Twig (and all php) is processed server-side before javascript even gets initiated. And once the page renders in the browser there no way to interact directly with twig. The only two options that you have is to either 1) reload the page or 2) request a page segment via ajax. I would opt for the later in this case.</p>

<p>To do this, you could either listen for an <code>onClick()</code> event on a submit button of some kind, or listen for <code>onChange()</code> events on the drop-down menus themselves; which would trigger an ajax call to the server, and display the results in your placeholder 'div'.</p>

<p>On the craft side you would want to create a separate template that renders just the requested news articles, based on either: variables that you pass to the template via post; or using a custom route that you set up (i.e. '/news/2015/03/').</p>

<p>For an example of creating a page template using custom routes and ajax to load entries based on date, <a href="https://craftcms.stackexchange.com/a/5456/784">see this example</a>. You will need to adapt it slightly to only use the month and year (vs. month, day, year).</p>

<p>For an ajax example, <a href="https://craftcms.stackexchange.com/a/4528/784">see this example</a> — which links to additional examples.</p>

<p>If you have a hard time putting the pieces together, leave a comment and I'll try and provide a more specific example.</p>

CC BY-SA 3.0
2015-04-04T22:22:47.120Z
2015-04-04T23:23:45.700Z
2017-04-13T12:51:00.303Z
Douglas McDonald
<p>I’m creating an archive of news article entries. For that purpose, I’m trying to get a value from a dropdown (user-selected year and month) and then assign it to a pagination procedure that calls up all articles of that particular year and month.</p>

<p>However, from what I’ve read so far, you cannot pass a Javascript variable to Twig. Is there any other possibility of passing on user selection to Twig?</p>

<p>Thanks! :)</p>

<pre><code>&lt;div class="dropdown" style="float:left;"&gt;
&lt;button class="btn btn-default dropdown-toggle text-sans" type="button" id="selectYear" data-toggle="dropdown"&gt;Year &lt;span class="caret"&gt;&lt;/span&gt;&lt;/button&gt;
&lt;ul class="dropdown-menu text-sans" role="menu" aria-labelledby="selectYear"&gt;
    &lt;li role="presentation"&gt;&lt;a role="menuitem" tabindex="-1" href="#"&gt;2015&lt;/a&gt;&lt;/li&gt;
    &lt;li role="presentation"&gt;&lt;a role="menuitem" tabindex="-1" href="#"&gt;2014&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;div class="dropdown" style="float:left; margin:0 0 0 10px;"&gt;
&lt;button class="btn btn-default dropdown-toggle text-sans" type="button" id="selectMonth" data-toggle="dropdown"&gt;Month &lt;span class="caret"&gt;&lt;/span&gt;&lt;/button&gt;
&lt;ul class="dropdown-menu text-sans" role="menu" aria-labelledby="selectMonth"&gt;
    &lt;li role="presentation"&gt;&lt;a role="menuitem" tabindex="-1" href="#"&gt;January&lt;/a&gt;&lt;/li&gt;
    &lt;li role="presentation"&gt;&lt;a role="menuitem" tabindex="-1" href="#"&gt;February&lt;/a&gt;&lt;/li&gt;
    ...
    &lt;li role="presentation"&gt;&lt;a role="menuitem" tabindex="-1" href="#"&gt;December&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;

&lt;script type="text/javascript"&gt;
$('select[id="selectYear"]').change(function(){
    var e = document.getElementById("selectYear");
    var JSYEAR = e.options[e.selectedIndex].text;
})
$('select[id="selectMonth"]').change(function(){
    var e = document.getElementById("selectMonth");
    var JSMONTH = e.options[e.selectedIndex].text;
})
&lt;/script&gt;

{% set firstDayOfMonth = JSYEAR ~ "-" ~ JSMONTH ~ "-01" %}
{% set daysInThisMonth = firstDayOfMonth|date('t') %}
{% set lastDayOfMonth  = JSYEAR ~ "-" ~ JSMONTH ~ "-" ~ daysInThisMonth %}
{% set thisMonthsEntries = craft.entries.section(category).postDate('and', '&gt;= ' ~ firstDayOfMonth, '&lt;= ' ~ lastDayOfMonth) %}
{% paginate thisMonthsEntries as pageEntries %}
   {% for date, articles in pageEntries | group("postDate|date('F Y')") %}
       ...
</code></pre>

<templating><pagination><javascript>
Get value from dropdown and use in Twig
# Get value from dropdown and use in Twig

(Asked by: username_0 on 2015-04-04)

I’m creating an archive of news article entries. For that purpose, I’m trying to get a value from a dropdown (user-selected year and month) and then assign it to a pagination procedure that calls up all articles of that particular year and month.
However, from what I’ve read so far, you cannot pass a Javascript variable to Twig. Is there any other possibility of passing on user selection to Twig?
Thanks! :)

Year 

    2015
    2014



Month 

    January
    February
    ...
    December





{% set firstDayOfMonth = JSYEAR ~ "-" ~ JSMONTH ~ "-01" %}
{% set daysInThisMonth = firstDayOfMonth|date('t') %}
{% set lastDayOfMonth  = JSYEAR ~ "-" ~ JSMONTH ~ "-" ~ daysInThisMonth %}
{% set thisMonthsEntries = craft.entries.section(category).postDate('and', '>= ' ~ firstDayOfMonth, '<= ' ~ lastDayOfMonth) %}
{% paginate thisMonthsEntries as pageEntries %}
   {% for date, articles in pageEntries | group("postDate|date('F Y')") %}
       ...

**Tags:** templating, pagination, javascript

---

## 1 answers

### Answer by username_1 on 2015-04-04. Score: 4

Twig (and all php) is processed server-side before javascript even gets initiated. And once the page renders in the browser there no way to interact directly with twig. The only two options that you have is to either 1) reload the page or 2) request a page segment via ajax. I would opt for the later in this case.

To do this, you could either listen for an `onClick()` event on a submit button of some kind, or listen for `onChange()` events on the drop-down menus themselves; which would trigger an ajax call to the server, and display the results in your placeholder 'div'.

On the craft side you would want to create a separate template that renders just the requested news articles, based on either: variables that you pass to the template via post; or using a custom route that you set up (i.e. '/news/2015/03/').

For an example of creating a page template using custom routes and ajax to load entries based on date, [see this example](https://craftcms.stackexchange.com/a/5456/784). You will need to adapt it slightly to only use the month and year (vs. month, day, year).

For an ajax example, [see this example](https://craftcms.stackexchange.com/a/4528/784) — which links to additional examples.

If you have a hard time putting the pieces together, leave a comment and I'll try and provide a more specific example.


---

