Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-04-04T22:22:47.120Z
Answers.LastActivityDate: 2015-04-04T23:23:45.700Z
Answers.LastEditDate: 2017-04-13T12:51:00.303Z
Answers.UserDisplayName: Douglas McDonald
Body: <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>
  
Tags: <templating><pagination><javascript>
Title: Get value from dropdown and use in Twig
ThreadText: # 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! :)
  
  ```
  <div class="dropdown" style="float:left;">
  <button class="btn btn-default dropdown-toggle text-sans" type="button" id="selectYear" data-toggle="dropdown">Year <span class="caret"></span></button>
  <ul class="dropdown-menu text-sans" role="menu" aria-labelledby="selectYear">
      <li role="presentation"><a role="menuitem" tabindex="-1" href="#">2015</a></li>
      <li role="presentation"><a role="menuitem" tabindex="-1" href="#">2014</a></li>
  </ul>
  </div>
  <div class="dropdown" style="float:left; margin:0 0 0 10px;">
  <button class="btn btn-default dropdown-toggle text-sans" type="button" id="selectMonth" data-toggle="dropdown">Month <span class="caret"></span></button>
  <ul class="dropdown-menu text-sans" role="menu" aria-labelledby="selectMonth">
      <li role="presentation"><a role="menuitem" tabindex="-1" href="#">January</a></li>
      <li role="presentation"><a role="menuitem" tabindex="-1" href="#">February</a></li>
      ...
      <li role="presentation"><a role="menuitem" tabindex="-1" href="#">December</a></li>
  </ul>
  </div>
  
  <script type="text/javascript">
  $('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;
  })
  </script>
  
  {% 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.
  
  
  ---
  
  