Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Append a new row to a table using jQuery [duplicate]

I'm using the following code trying to append a new row to jquery but it's not working.

$('#search-results > tbody').append('<tr><td data-th="Name">Test</td><td data-th="Email">[email protected]</td><td data-th="Phone Number">07777777777</td><td data-th="Car Reg.">ocf83or</td><td data-th="Time">1pm</td></tr>');

My table is setup like so:

<table class="view-bookings-table mobile-optimised hidden" id="search-results">
    <thead>
        <tr>
            <th scope="col">Name</th>
            <th scope="col">Email</th>
            <th scope="col">Phone Number</th>
            <th scope="col">Car Reg.</th>
            <th scope="col">Time</th>
        </tr>
    </thead>
    <tbody></tbody>
</table>

EDIT

My full code is actually:

$('#search').submit(function(event) {
    event.preventDefault();
    $('#search-results > tbody').append('<tr><td data-th="Name">Test</td><td data-th="Email">[email protected]</td><td data-th="Phone Number">07777777777</td><td data-th="Car Reg.">ocf83or</td><td data-th="Time">1pm</td></tr>');
});
like image 607
V4n1ll4 Avatar asked Sep 27 '22 16:09

V4n1ll4


1 Answers

Your code is fine and works well with every possible version of jQuery.

demo

You may have a jQuery error somewhere else on your page or you are not loading it. Also what is

class='hidden' 

doing? Maybe it's just a css issue?

like image 185
boszlo Avatar answered Oct 24 '22 04:10

boszlo