vs14
vs14

Reputation: 41

Not able to pass JSON object as second argument in javascript function call

I am generating rows in javascript with for each loop, where each row has an anchor and with onclick of that anchor I am trying to pass a JSON object as second argument in my javascript function but I am getting undefined error.

I have already verified that result is a JSON object Here is my code to generate rows.

 _.each(
response.details,
function(result, key, list) {searchdetails += "<tr><td style=\"text-align:center\" ><a class=\"btn btn-primary\" type=\"button\" onClick='Updatedetails(\"2\",result)'>Details</a></td></tr>";
   });

I am getting below error:- enter image description here

I have also tried by appending it like string as below:-

 _.each(
response.details,
function(result, key, list) {searchdetails += "<tr><td style=\"text-align:center\" ><a class=\"btn btn-primary\" type=\"button\" onClick='Updatedetails(\"2\","+result+")'>Details</a></td></tr>";
   });

With this, I am getting below error:- enter image description here

Can anybody tell me how do I pass it as JSON object.

Upvotes: 0

Views: 187

Answers (1)

Sebastian Kaczmarek
Sebastian Kaczmarek

Reputation: 8515

You are missing quotes and need to stringify your object:

function(result, key, list) {searchdetails += "<tr><td style=\"text-align:center\" ><a class=\"btn btn-primary\" type=\"button\" onClick='Updatedetails(\"2\",\""+JSON.stringify(result)+"\")'>Details</a></td></tr>";

Note how modified code differs:

"...'Updatedetails(\"2\",\""+JSON.stringify(result)+"\")'..."

Upvotes: 2

Related Questions