Ctpelnar1988 Ctpelnar1988 - 1 year ago 213
jQuery Question

JavaScript: Uncaught SyntaxError: missing ) after argument list

When I try to pass a string as an argument in the following function, I get the

Uncaught SyntaxError: missing ) after argument list
error. It works fine when passing integers though. I am confused because I read a string must be passed as the argument, and not an integer.


r.title = "The large item"
counter = <integer>

<% @items.each do |r| %>
<p>Title: <%= r.title %></p>
<p>Price: <%= r.price %></p>
<p>Description: <%= r.description %></p>
<p style="color:blue;" class="room_<%= counter %>" onclick='addItemToCart(<%= r.title %>, <%= counter %>)'>Select Item</p>
<% end %>

When I pass
in place of
, the code works.


<script text/javascript>
function addItemToCart(title, item_number){
$("#" + item_number).append("<br>"+title);

Answer Source

I think you're missing quotes around the string. Try this:

<p style="color:blue;" class="room_<%= counter %>"
   onclick='addItemToCart("<%= r.title %>", <%= counter %>)'>Select Item</p>


To understand the issue, try looking at the HTML it outputs. I imagine you'll see something like this:

<p style="color:blue;" class="room_123"
   onclick='addItemToCart(The large item, 123)'>Select Item</p>

Hopefully, the missing quotes are obvious from that output.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download