Function does not seem to be executing on click

Well... the tittle says it. Here, I leave you the link. I lost nearly 4 hours trying to make it work!

I want to add rows to the table, when I click the button. But it does not seem to do anything at all.

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 the 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");    
}

Upvotes: 4

Views: 44

Answers (1)

Rory McCrossan
Rory McCrossan

Reputation: 337714

The string concatenation character in Javascript is +, not .. Also note that your original fiddle did not include jQuery. Try this:

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 fiddle

A better alternative would be to attach your events using Javascript to avoid the outdated on* attributes. Something like this:

<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");
    });
});

Example fiddle

Upvotes: 5

Related Questions