1

I have a problem, I'm trying to dynamically add some html via javascript, and the html has a js function that's supposed to trigger when it's clicked, but I keep getting this error no matter what I do Uncaught SyntaxError: Unexpected token } Has anyone had this problem before?

$("#addVehicle").click(function () {
    vehicleQueue += '<li class="ui-state-default">' + $("#years option:selected").text() + ' - ' + $("#make option:selected").text() + ' - ' + $("#model option:selected").text() + ' - ' + $("#trim option:selected").text() + '<span><img onclick="javascript:alert("Hola");" class="removeVehicle" width="25px" src="http://haveagreatholiday.com/img/popup-closeButton.png"></span></li>';

    $("#vehicleQueue").html('<ul id="sortable">' + vehicleQueue + '</ul>');
    vehicleJson.push({
        vehicleCount: vehicleCount,
        vehicleYear: $("#years option:selected").text(),
        vehicleMake: $("#make option:selected").text(),
        vehicleModel: $("#model option:selected").text(),
        vehicleTrim: $("#trim option:selected").text()
    });
    vehicleCount++;
    });    

This is my head:

<head>
<script type="text/javascript" src="view.js"></script>
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
<script src="functions.js"></script>
<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css" />
<link rel="stylesheet" type="text/css" href="view.css" media="all">
<style>
  #sortable { list-style-type: none; margin: 0; padding: 0; width: 97%; }
  #sortable li { margin: 0 3px 3px 3px; padding: 0.4em; padding-left: 1.5em; font-size: 1.2em; height: 18px; }
  #sortable li span {width: 16px;height: 17px;display: block;text-decoration: none;position: absolute;right: 18px;top: 3px;}
</style>

</head>
8
  • 2
    share your complete script tag, there is an syntactical error in your script Commented May 23, 2013 at 2:56
  • @ArunPJohny I added the whole script. Commented May 23, 2013 at 2:58
  • This portion looks fine, check in your browser console where the error is coming from (line). The error is coming from somewhere else Commented May 23, 2013 at 2:59
  • @ArunPJohny The browser console is giving the error message Uncaught SyntaxError: Unexpected token }. Commented May 23, 2013 at 3:01
  • along with the message it will show the filename and line number Commented May 23, 2013 at 3:01

1 Answer 1

12

Your onclick event handler has string escape problems, escape is as

onclick="alert(\'Hola\');"
Sign up to request clarification or add additional context in comments.

1 Comment

This was so unguessable!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.