0

Can't send multiple string value as parameter to javascript function

My server side coding looks like :

_

json += "<td><input type='checkbox' id='chkBoxHelp' onclick='chkbox('" + dt.Rows[i][1].ToString() + "','" + dt.Rows[i][2].ToString() + "');'";

And my client side coding looks like :

function chkbox(id,name) {
alert(id,name);
}
1
  • I've added the missing "; to the end of your statement, since it isn't the problem but was incorrect. Commented Aug 26, 2017 at 10:21

1 Answer 1

1

Since you've quoted your attribute value with ', it ends at the first ' inside it.

Instead, use ":

json += "<td><input type='checkbox' id='chkBoxHelp' onclick='chkbox(\"" + dt.Rows[i][1].ToString() + "\",\"" + dt.Rows[i][2].ToString() + "\");'";
// -----------------------------------------------------------------^^--------------------------------^^-^^--------------------------------^^

Or, of course, don't use inline onxyz-attribute-style handlers at all, as they have several issues, not least that functions like your chkBoxHelp have to be globals.

For instance, you might store the arguments as data-* values:

json += "<td><input type='checkbox' id='chkBoxHelp' data-id='" + dt.Rows[i][1].ToString() + "' data-name='" + dt.Rows[i][2].ToString() + "'";
// -------------------------------------------------^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

...and use modern event handling (addEventListener, etc.) to hook up the function, and have the function retrieve the values from the element.

Sign up to request clarification or add additional context in comments.

Comments

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.