0

I am trying to pass parameters to function using single quote however it is showing error due to missing quote but I am unable to figure out where it is I am using inside datatable

onclick="assign_id(' + aData['id'] + ',string value' + ')"





expected output:
     onclick="assign_id('dbvalue','string')"

 "fnCreatedRow": function (nRow, aData, iDataIndex)
            {
    var action_link = '<a class="dropdown-item" onclick="assign_id(' + aData['db value here'] + ',' some string here ')">Delivered</a>';
},
faraz
  • 223
  • 2
  • 11
  • Does this answer your question? [Pass a string parameter in an onclick function](https://stackoverflow.com/questions/9643311/pass-a-string-parameter-in-an-onclick-function) – Shahnawaz Hossan Dec 24 '20 at 14:07
  • @ShahnawazHossan I am unable to get how to place this, could u write here with above requirement – faraz Dec 24 '20 at 14:08
  • 1
    Everything within double quotes is just a string. Instead of using `onclick` attribute, i suggest that you use `element.addEventListener('click', ...)` to add the click event listener. – Yousaf Dec 24 '20 at 14:17
  • this should work onclick="assign_id(aData[id], '2')" – lissettdm Dec 24 '20 at 14:25
  • Don't use inline event handlers. It's widely considered insecure, bad coding and causes exactly these problems. The same is true for creating HTML from a string. – connexo Dec 24 '20 at 14:33
  • Look at your onclick function. It is currently `assign_id( ' + aData['id'] + ',string value' + ' )`. If you don't see anything wrong, read this again and try matching the quotes. – Jeremy Thille Dec 24 '20 at 14:36

2 Answers2

1

Maybe this should work?

var action_link = '<a class="dropdown-item" onclick="assign_id(\'' + aData['db value here'] + '\', \'' + 2 + '\')">Delivered</a>';

Or using template strings

onclick=`assign_id('${aData['id']}',2)`;
Sudhanshu Kumar
  • 1,926
  • 1
  • 9
  • 21
0

I don't know what aData['id'] refers to because it's can only be a string. Try this:

onclick="assign_id('aData[`id`],2')"
FahDev
  • 357
  • 1
  • 7