Search code examples
javascriptstringurlsyntaxencapsulation

SyntaxError: missing ) after argument list when onClick calling function and passing url as first argument. Javascript


When passing a URL through the first parameter of a function called cancelOrderByUrl in the "onClick" I get this error:

SyntaxError: missing ) after argument list

However, if a number was to be passed into the first parameter of cancelOrderByUrl(**HERE**, not here, not here); there is no error and my function executes. So it has to do with syntax, how can i make sure the URL is encapsulated as a string? toString(); does not do the trick. Any suggestions?

<script src="someScriptThatContainsFunctionBeingCalledInOnClick.js" charset="utf-8"></script> 
<script>
var someArray = [{cancel:"hhtps://api.somedomain.com/thisandthat/numbers", quantity:"12", price: "2.74"},{cancel:"hhtps://api.somedomain.com/thisandthat/numbers", quantity:"10", price: "2.71"}];
for (var i = 0; i < someArray.length; i++){

var orderDetails = '<td data-th="Cancel">' + '<button class="btn btn-cancel-order" onclick="cancelOrderByUrl('+someArray[i].cancel+', '+parseFloat(someArray[i].quantity).toFixed(2)+', '+parseFloat(someArray[i].price).toFixed(2)+')" >X</button>' + "</td>";

document.getElementById(someArray[i].created_at).innerHTML = orderDetails;

};
</script>

//if a number is passed instead of url, no problem


Solution

  • You can try using string.Format() function.

    onclick='@string.Format("cancelOrderByUrl(YOUR PARAMETERS)")'