How to Pass string parameter in an onclick function

By FoxLearn 2/16/2024 9:37:55 AM   574
This post shows you how to pass string parameter in an onclick function javascript.

For example:

<a href="#" onclick="return getByCustomerID('+ item.customerID + ')">Edit</a>

If customerID is a string

ex. 'ABCD', 'XYZS'

You will get an error when passing value to onclick function

html render

<a href="#" onclick="return getByCustomerID(ABCD)">Edit</a>

To solve the problem, You just need to add some quotes around item.customerID

You can modify your code as shown below.

<a href="#" onclick="return getByCustomerID(\''+ item.customerID + '\')">Edit</a>