Reputation: 41
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 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:-
Can anybody tell me how do I pass it as JSON object.
Upvotes: 0
Views: 187
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