Search code examples
javascriptgrailsgsp

JavaScript not working in Grails/GSP


I have the following JavaScript code:

    <script type="text/javascript"><!--
​$(function () {
    $('#add').click(function() {
        $(this).before($('select:eq(0)').clone());
      if ($('select').length > 5) $(this).hide();        
    });
});​
//-->
</script>

Here is the HTML code:

<select name="dropdown">
    <option value="a">Apple</option>
    <option value="b">Bee</option>
    <option value="c">Cat</option>
    <option value="d">Donkey</option>
    <option value="e">Elephant</option>
</select>
<a href="#" id="add">Add</a>

In the jsfiddle demo its working fine, but its not working on my gsp. Any idea or I am missing out something? Whenever I click on the "Add" it just display /# on my url.

The error message I managed to figure out through inspecting the element is as follows: Uncaught SyntaxError: Unexpected token ILLEGAL

Error Message Screenshot Console error screenshot from Chrome


Solution

  • There are probably some illegal characters in the <script> block.

    Did you copy/paste it from somewhere else? I would suggest you put cursor at the start and end of each line and delete all characters till you are sure there are no non-visible characters.

    Alternatively delete the whole script block and retype it manually