Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-05-03T15:02:22.227Z
Answers.Text: Great, thank you so much!!!
Answers.UserDisplayName: Samuel
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-05-03T14:36:17.253Z
Answers.LastActivityDate: 2022-05-03T14:36:17.253Z
Answers.UserDisplayName: pixleight
Body: <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>
  
Tags: <javascript><escaping><escape>
Title: Escaping quotes of an entry string used inside a script tag
ThreadText: # 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&#039;s &quot;string&quot;` which in javascript if the variable is shown on screen will show all the characters of the html entities.
  
  I have tried:
  
  ```
  <script>
     var string = '{{ creatif.creatifsName|raw }}'
  </script>
  
  ```
  
  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
  
  ```
  <script>
     var mystring = '{{ creatif.creatifsName|escape('js') }}'
  </script>
  
  ```
  
  <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!!!  
  
  ---
  
  