<p>The Twig code to fetch entries occurs on page load - Your JS code will run after the page loads and won't be able to update/fetch new entries. Instead, you should look at building a system that uses query strings to add the id's to the Twig variable on page load - then enhance this using AJAX.</p>

CC BY-SA 3.0
2016-08-01T17:20:34.907Z
2016-08-01T17:20:34.907Z
Luke Pearce
<p>I have a filter system on my website which uses categories. The categories have all got their own ID tagged to their HTML Element. Now I just need to get the clicked checkboxes to add their ID to the Twig variable which displays the entries in the for loop.</p>

<pre><code>{% set categories = craft.categories.section('retrospect') %}

{% set relatedCategories = [] %}

{% for category in categories %}
  {% set relatedCategories = relatedCategories|merge([taggedIds]) %}
{% endfor %}

{% set related = craft.categories.section('retrospect').id(relatedCategories) %}

{% set relatedArticles = craft.entries.section('retrospect').relatedTo(related) %}

{% for entry in relatedArticles.order('caseNumber asc').after('2016-01').before('2016-12') %}
  {{ entry.title }}
{% endfor %}
</code></pre>

<templating><categories><filter>
Set Twig variable with javascript/jquery for displaying entries based on clicked value
# Set Twig variable with javascript/jquery for displaying entries based on clicked value

(Asked by: username_0 on 2016-08-01)

I have a filter system on my website which uses categories. The categories have all got their own ID tagged to their HTML Element. Now I just need to get the clicked checkboxes to add their ID to the Twig variable which displays the entries in the for loop.
{% set categories = craft.categories.section('retrospect') %}

{% set relatedCategories = [] %}

{% for category in categories %}
  {% set relatedCategories = relatedCategories|merge([taggedIds]) %}
{% endfor %}

{% set related = craft.categories.section('retrospect').id(relatedCategories) %}

{% set relatedArticles = craft.entries.section('retrospect').relatedTo(related) %}

{% for entry in relatedArticles.order('caseNumber asc').after('2016-01').before('2016-12') %}
  {{ entry.title }}
{% endfor %}

**Tags:** templating, categories, filter

---

## 1 answers

### Answer by username_1 on 2016-08-01. Score: 2

The Twig code to fetch entries occurs on page load - Your JS code will run after the page loads and won't be able to update/fetch new entries. Instead, you should look at building a system that uses query strings to add the id's to the Twig variable on page load - then enhance this using AJAX.


---

