The function does not seem to execute when clicked

Well ... tilt talks about this. Here I leave you a link. I lost almost 4 hours trying to get it to work!

I want to add rows to a table when I click a button. But that doesn't seem to do anything.

http://jsfiddle.net/QqMsX/24/

<table class="table table-bordered">
<thead>
 <tr>
     <th>Relation</th>
     <th>Column1</th>
     <th>Column2</th>
     <th>Column3</th>
     <th>Column4</th>
     <th>Column5</th>
</tr>
</thead>
    <tbody id = 'FamilyTable'>                          
    </tbody>
</table>                            

<button onclick ="AddRow()" type="button" class="btn btn-primary">Add</button>

And JavaScript code.

function AddRow() 
{
    var Row = '<tr>'.
               '<td>Data</td>'.
               '<td>Data</td>'.
               '<td>Data</td>'.
               '<td>Data</td>'.
               '<td>Data</td>'.
               '<td>Data</td>'.
               '</tr>'; 

    $(Row).appendTo("#FamilyTable");    
}
+4
source share
1 answer

The javascript string concatenation character is +, rather than .. Also note that your original fiddle did not include jQuery. Try the following:

function AddRow() {
    var row = '<tr>' +
        '<td>Data</td>' +
        '<td>Data</td>' +
        '<td>Data</td>' +
        '<td>Data</td>' +
        '<td>Data</td>' +
        '<td>Data</td>' +
        '</tr>';
    $(row).appendTo("#FamilyTable");
}

Updated script

Javascript, on*. - :

<button type="button" class="btn btn-primary">Add</button>
$(function() {
    $('button').click(function() {
        var row = '<tr>' +
            '<td>Data</td>' +
            '<td>Data</td>' +
            '<td>Data</td>' +
            '<td>Data</td>' +
            '<td>Data</td>' +
            '<td>Data</td>' +
            '</tr>';
        $(row).appendTo("#FamilyTable");
    });
});

+5

Source: https://habr.com/ru/post/1624311/


All Articles