<p>That is the correct way of doing it but you need to be sure that <code>entry</code> is a variable and set to an entry model at that point.  And that you're in an appropriate part of your template in general for outputting the code.</p>

<p>E.g. this works for analytics for example, placed at the end of my main structure block.</p>

<pre><code>{# ANALYTICS GOOGLE #}
{% set js %}
&lt;!-- Google Code for Website Conversions Conversion Page --&gt;
&lt;script type="text/javascript"&gt;
/* &lt;![CDATA[ */
var google_conversion_id = XXXXXXXXX;
var google_conversion_language = "en";
var google_conversion_format = "3";
var google_conversion_color = "ffffff";
var google_conversion_label = "XXXXXXXXXX";
var google_conversion_value = {{order.totalPrice}};
var google_conversion_currency = "AUD";
var google_remarketing_only = false;
/* ]]&gt; */
&lt;/script&gt;
&lt;script type="text/javascript" src="//www.googleadservices.com/pagead/conversion.js"&gt;
&lt;/script&gt;
&lt;noscript&gt;
&lt;div style="display:inline;"&gt;
&lt;img height="1" width="1" style="border-style:none;" alt="" src="//www.googleadservices.com/pagead/conversion/XXXXXXX/?value={{order.totalPrice}}&amp;amp;currency_code=AUD&amp;amp;label=XXXXXXXXX&amp;amp;guid=ON&amp;amp;script=0"/&gt;
&lt;/div&gt;
&lt;/noscript&gt;

{% endset %}

{% includejs js %}
</code></pre>

CC BY-SA 3.0
2016-11-17T13:49:40.803Z
Entry is defined.  I have no template errors and the rest of the template works.  This is the only thing that does not, and I assume its because I am inserting a Twig into a javascript function inside of a `script` tag.  See update to original question above the full include statement.
Hellyar
CC BY-SA 3.0
2016-11-19T08:04:14.543Z
See my comment above.  In general it does work, so I need a bit more info to work out what you're doing and where it might be going wrong.
Jeremy Daalder
CC BY-SA 3.0
2016-11-17T05:20:20.473Z
2016-11-17T05:20:20.473Z
Jeremy Daalder
<p>I just want to print a value from Craft into a JavaScript function to filter a dashboard.  </p>

<p>The function is this:</p>

<pre><code>showOnly(filterName, values);
</code></pre>

<p>I tried this:</p>

<pre><code>showOnly('Short Names','{{ entry.title }}');
</code></pre>

<p>But that did not work.  And yes, the filter does work If I hard wire the value that Craft/Twig should print.  But, I need it to match the title of the page as one navigates from entry to entry.</p>

<p><strong>UPDATE</strong></p>

<p>Here is the actual <code>showOnly</code> function in its entirety:</p>

<pre class="lang-javascript prettyprint-override"><code>function showOnly(filterName, values) {
    // Think a single select filter.
    sheet = mainViz.getWorkbook().getActiveSheet();
    if(sheet.getSheetType() === 'worksheet') {
        sheet.applyFilterAsync(filterName, values, 'REPLACE');
    } else {
        // for dashboards
        worksheetArray = sheet.getWorksheets();
        for(var i = 0; i &lt; worksheetArray.length; i++) {
            worksheetArray[i].applyFilterAsync(filterName, values, 'REPLACE');
        }
    }
};
</code></pre>

CC BY-SA 3.0
2016-11-16T22:11:22.670Z
And what value should  it print? String or array of strings (as values parameter name suggests)? Show us what it currently prints;
Krzysztof Boduch
CC BY-SA 3.0
2016-11-17T13:44:00.630Z
I don't know what it is "printing" to the function.  I can't see it.  The dashboard filters to nothing.  How do I add a console event to depict what is being printed (if anything).  I added the actual function to the question above...  Thank you
Hellyar
CC BY-SA 3.0
2016-11-17T23:35:22.570Z
I think we need more info about the context here to help you - is this a front end dashboard, or in a back end plugin?  What exactly is not working - can you inspect the source and see the twig replacing occurring (you should be able to).
Jeremy Daalder
<templating><javascript><twigextension>
How do I print a value from Craft/Twig into a Javascript function?
# How do I print a value from Craft/Twig into a Javascript function?

(Asked by: username_0 on 2016-11-16)

I just want to print a value from Craft into a JavaScript function to filter a dashboard.  
The function is this:
showOnly(filterName, values);

I tried this:
showOnly('Short Names','{{ entry.title }}');

But that did not work.  And yes, the filter does work If I hard wire the value that Craft/Twig should print.  But, I need it to match the title of the page as one navigates from entry to entry.
UPDATE
Here is the actual showOnly function in its entirety:
function showOnly(filterName, values) {
    // Think a single select filter.
    sheet = mainViz.getWorkbook().getActiveSheet();
    if(sheet.getSheetType() === 'worksheet') {
        sheet.applyFilterAsync(filterName, values, 'REPLACE');
    } else {
        // for dashboards
        worksheetArray = sheet.getWorksheets();
        for(var i = 0; i < worksheetArray.length; i++) {
            worksheetArray[i].applyFilterAsync(filterName, values, 'REPLACE');
        }
    }
};

**Tags:** templating, javascript, twigextension
> **Comments:**
> **username_1**: And what value should  it print? String or array of strings (as values parameter name suggests)? Show us what it currently prints;  
> **username_0**: I don't know what it is "printing" to the function.  I can't see it.  The dashboard filters to nothing.  How do I add a console event to depict what is being printed (if anything).  I added the actual function to the question above...  Thank you  
> **username_2**: I think we need more info about the context here to help you - is this a front end dashboard, or in a back end plugin?  What exactly is not working - can you inspect the source and see the twig replacing occurring (you should be able to).  

---

## 1 answers

### Answer by username_2 on 2016-11-17. Score: 2 | Accepted answer

That is the correct way of doing it but you need to be sure that entry is a variable and set to an entry model at that point.  And that you're in an appropriate part of your template in general for outputting the code.
E.g. this works for analytics for example, placed at the end of my main structure block.
{# ANALYTICS GOOGLE #}
{% set js %}









{% endset %}

{% includejs js %}

> **Comments:**
> **username_0**: Entry is defined.  I have no template errors and the rest of the template works.  This is the only thing that does not, and I assume its because I am inserting a Twig into a javascript function inside of a `script` tag.  See update to original question above the full include statement.  
> **username_2**: See my comment above.  In general it does work, so I need a bit more info to work out what you're doing and where it might be going wrong.  

---

