0

I want to dynamically print values and strings on my Twig template using JavaScript code. But it doesn't work.

Also when I load the Twig template, it should show the contents of the "test" on page, which is just "asdasd".

But it doesn't work. What I get is nothing, as if no script code exists.

Here's the code I have on my Twig template:

  <div id="output">
  </div>
  <script type="text/javascript">
      var $queryString;
      $queryString = queryString.replace(new RegExp("pageno" + "=\\w+"),"").replace("?&","?").replace("&&","&");
      var test;
      test = "asdasd";
      document.getElementById('output').innerHTML = test;
      alert("HHHHhhhhhhHHHALSDJLKJ");
  </script>

I don't get any alert either.

I did follow: https://www.guru99.com/introduction-to-javascript.html

I just did read in this link: Why does jQuery or a DOM method such as getElementById not find the element?

that we have to add before the script. I did that too, but still it doesn't work.

Why the varible won't show when I load the page ?

Coder88
  • 769
  • 2
  • 7
  • 17
  • Why the downvote? – Coder88 Apr 24 '20 at 14:05
  • I assume the downvote because the question is poorly detailed. You posted code without explaining what it's doing and what it's supposed to be doing. Posting links to a guide that you were following does not count as an explanation. Try and follow the [guide](https://stackoverflow.com/help/how-to-ask). – Dane Brouwer Apr 24 '20 at 14:17
  • Now I added some explanation, what it's doiing and what it's supposed to do. – Coder88 Apr 24 '20 at 14:22

1 Answers1

1

The reason nothing is showing is because there's an error in your code. Try and open up your developer tools to see if there are any errors.

To fix the issues all you need to do is give queryString$ a value.

var $queryString = "";

and replace queryString.replace with queryString$.replace.

<div id="output">
</div>
<script type="text/javascript">
  var $queryString = "";
  $queryString = $queryString.replace(new RegExp("pageno" + "=\\w+"), "").replace("?&", "?").replace("&&", "&");
  var test;
  test = "asdasd";
  document.getElementById('output').innerHTML = test;
  alert("HHHHhhhhhhHHHALSDJLKJ");
</script>
Community
  • 1
  • 1
Dane Brouwer
  • 1,974
  • 1
  • 17
  • 21