user2576631
user2576631

Reputation: 13

jQuery selector children

I need to select tbody and insert some tr inside this section:

<div id="retour">
<table>
    <thead>
    </thead>
    <tbody>
    </tbody>
</table>
</div>

I have tried the following code, but it didn't work:

var table = $('#retour > table > tbody').children();
table.append('<tr>');
table.append( '<td>' + 'MyInfos' + '</td>' );
table.append('</tr>');

Upvotes: 1

Views: 104

Answers (6)

Vito
Vito

Reputation: 1090

you need to use :

<head>
        <script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
        <script type="text/javascript">
            function addTr(){
                var table = $('#retour > table > tbody');
                table.append('<tr>');
                table.append( '<td>' + 'MyInfos' + '</td>' );
                table.append('</tr>');
            }
        </script>
</head>
<body>
    <div id="retour">
        <button onclick="addTr()">Add</button>
        <table>
            <thead>             </thead>
            <tbody>
            </tbody>
        </table>
    </div>
</body>

Upvotes: 0

Sam
Sam

Reputation: 129

you can try this

var trs = '<tr>';
trs += '<td>' + 'MyInfos' + '</td>';
trs += '<tr>';

$('#retour tbody').html(trs);

Upvotes: 0

Pieter
Pieter

Reputation: 1833

You can leave the .children(). There are no children in your tbody yet.

Upvotes: 0

palaѕн
palaѕн

Reputation: 73906

Try this:

var table = $('#retour tbody');
table.append('<tr><td>' + 'MyInfos' + '</td></tr>');

FIDDLE DEMO

Upvotes: 1

mishik
mishik

Reputation: 10003

Shortly, to get tbody use this:

var table = $('#retour > table > tbody');

To append new cell:

table.append('<tr><td>' + 'MyInfos' + '</td></tr>');

This:

var table = $('#retour > table > tbody').children();

Will return you a list of tr elements, rather than tbody. And it's tbody that you want to append new tr's to.

Upvotes: 0

Arun P Johny
Arun P Johny

Reputation: 388316

i think you need

var table = $('#retour > table > tbody');
table.append('<tr><td>' + 'MyInfos' + '</td></tr>');

Upvotes: 0

Related Questions