1

Using javascript and Jquery, I am trying to add a button for each row. I want to add a click event to button of each row. Here is my code:

reportModal = UIkit.modal("#report_modal");
$('#report_modal').empty();

var gridHtml = "<table id='table1'>"
                  +"    <thead>"
                  +"      <tr>"
                  +"          <th>FirstName</th>"
                  +"          <th>Age</th>"
                  +"          <th>Tel</th>"                 
                  +"          <th>Button</th>"  
                  +"      </tr>"
                  +"      </thead>"
                  +"     <tbody></tbody>"
                  +"</table>";

$('#report_modal').html(gridHtml);                

var data = {};
       data.d = [{FirstName: 'AAA', Age: '20', Tel: '111'}, 
                {FirstName: 'BBB', Age: '98', Tel: '222'},
                {FirstName: 'CCC', Age: '45', Tel: '333'}];     
var trHTML = '';

for(var i = 0; i < data.d.length; i++){

  trHTML += '<tr><td>' + data.d[i].FirstName + '</td><td>' + data.d[i].Age + '</td><td>' + '<a onclick="showMsg(${data.d[i].FirstName})" href="#"><i class="flaticon-icon-path"></i></a>' + '</td></tr>';
}               

$('#report_modal').append(trHTML);

function showMsg(msg){
  alert(msg);
}

My problem is that when click in buttons the showMsg function is called but it show "undefined";

Please any idea to solve it. Thanks.

1
  • you've gotta use "Template literals" `` in order to use string interpolation or classic sctring concatenation <a onclick="showMsg(' + data.d[i].FirstName + '" Commented Jun 27, 2018 at 22:51

2 Answers 2

1

This is syntax error: use onclick="showMsg(\''+data.d[i].FirstName+'\')"

Like this:

   trHTML += '<tr><td>' + data.d[i].FirstName + '</td><td>' + data.d[i].Age + '</td><td>' + '<a onclick="showMsg(\''+data.d[i].FirstName+'\')" href="#"><i class="flaticon-icon-path"></i></a>' + '</td></tr>';
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks so much! That's exactly what I wanted.
1

you need to use "Template literals" `` in order to use string interpolation. Moreover in thiat case you can use multiline string!

trHTML += `<tr>
  <td>${data.d[i].FirstName}</td>
  <td>${data.d[i].Age}</td>
  <td>
    <a onclick="showMsg(${data.d[i].FirstName})" href="#">
      <i class="flaticon-icon-path"></i>
    </a>
  </td>
</tr>`;

or use classic string concatenation

trHTML += '<tr><td>' + data.d[i].FirstName + '</td><td>' + data.d[i].Age + '</td><td>' + '<a onclick="showMsg(' + data.d[i].FirstName + '" href="#"><i class="flaticon-icon-path"></i></a>' + '</td></tr>';

1 Comment

FYI jQuery supports this more readable syntax : row = $("<tr/">) , or td = $("<td/>") . And you can use append() (api.jquery.com/append) to dynamically add element to it.

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.