Ctpelnar1988 Ctpelnar1988 - 4 months ago 61
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.

HTML:

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>
<br>
<br>
<% end %>


When I pass
r.id
in place of
r.title
, the code works.

JavaScript:

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

Answer

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>

EDIT

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.

Comments