<pre><code>&lt;script&gt;
   var mystring = '{{ creatif.creatifsName|escape('js') }}'
&lt;/script&gt;
</code></pre>
<p><a href="https://twig.symfony.com/doc/2.x/filters/escape.html" rel="nofollow noreferrer">https://twig.symfony.com/doc/2.x/filters/escape.html</a></p>
<p>Remember to wrap your variable declaration in quotes for it to be set as a string in the JS.</p>

CC BY-SA 4.0
2022-05-03T15:02:22.227Z
Great, thank you so much!!!
Samuel
CC BY-SA 4.0
2022-05-03T14:36:17.253Z
2022-05-03T14:36:17.253Z
pixleight
<p>How can I display an entry string, with included special characters (like single and double quotes) , inside a script tag without translating these special characters into html entities but escaping their quotes?</p>
<p>By default: <code>Carlo's &quot;string&quot;</code> will output <code>Carlo&amp;#039;s &amp;quot;string&amp;quot;</code> which in javascript if the variable is shown on screen will show all the characters of the html entities.</p>
<p>I have tried:</p>
<pre><code>&lt;script&gt;
   var string = '{{ creatif.creatifsName|raw }}'
&lt;/script&gt;
</code></pre>
<p>Which removes the html entities but obviously the script will not work because the quotes are not escaped.</p>

<javascript><escaping><escape>
Escaping quotes of an entry string used inside a script tag
# Escaping quotes of an entry string used inside a script tag

(Asked by: username_0 on 2022-05-03)

How can I display an entry string, with included special characters (like single and double quotes) , inside a script tag without translating these special characters into html entities but escaping their quotes?
By default: Carlo's "string" will output Carlo's "string" which in javascript if the variable is shown on screen will show all the characters of the html entities.
I have tried:


Which removes the html entities but obviously the script will not work because the quotes are not escaped.

**Tags:** javascript, escaping, escape

---

## 1 answers

### Answer by username_1 on 2022-05-03. Score: 3 | Accepted answer

https://twig.symfony.com/doc/2.x/filters/escape.html
Remember to wrap your variable declaration in quotes for it to be set as a string in the JS.

> **Comments:**
> **username_0**: Great, thank you so much!!!  

---

