0
function createResendIVButton(userId, name){
    return '<span class=btn onclick=usersTable.resendInvestorVerification("'+userId+'","'+name+'")>resend&nbsp;I.V.</span>';
}

When creating an element as HTML, is it possible to somehow send the function handle instead of a string (function name)? String is inconvenient, because of visibility problems. And I need to create from HTML because library I use do not let me access its DOM easily, but allows me to initialize rows from HTML code.

exebook
  • 32,014
  • 33
  • 141
  • 226
  • Possible duplicate of [Add onClick event to document.createElement("th")](http://stackoverflow.com/questions/11017509/add-onclick-event-to-document-createelementth) – rishipuri May 08 '17 at 13:55
  • @rishipuri your reference suggests use of `appendChild()`, but I can only send HTML code to some function. – exebook May 08 '17 at 13:59
  • have you tried using `eval()` method? – quirimmo May 08 '17 at 14:03

2 Answers2

0

Creates a DOM element and bind on then a action:

        function createResendIVButton(userId, name){

            var button = document.createElement('span');
            button.className = 'btn';

            button.addEventListener('click', function(){

                console.log(userId);
                console.log(name);

            });

            button.innerHTML = 'click on me';

            return button;

        }
0

Like this?

function createResendIVButton(userId, name) {
  var span = document.createElement('span');
  span.innerHTML = 'resend I.V';
  span.onclick = function() {
    usersTable.resendInvestorVerfication(userId, name);
  }
  
  var div = document.getElementById('somecontent');
  div.appendChild(span);
}


createResendIVButton(1, 'Joe');
#somecontent {
  width: 100px;
  height: 100px;
  background: #ccc;
 }
<div id='somecontent'>
</div>
rishipuri
  • 1,498
  • 11
  • 18