1

My inputs:

<input id="id_name" name="name"/>
<input id="id_age" name="age"/>
<button id="add_btn"></button>

After every click on button add_btn I need append my table(<tr>) with form input value. At the start, the table is empty. Only <tbody>.

<table class="mytab">
 <tbody>
    <tr>
        <td> HERE_VALUE_FROM_NAME_FIELD</td>
        <td>HERE_VALUE_FROM_AGE_FIELD</td>
    </tr>
    <tr>
        <td> HERE_NEXT_VALUE_FROM_NAME_FIELD</td>
        <td>HERE_NEXT_VALUE_FROM_AGE_FIELD</td>
    </tr>
  </tbody>
</table>           

Onclick function:

$("#myform").on('click', "#add_btn", function(e) {
        e.preventDefault();
        //fill table
});

4 Answers 4

3

Try like this:

HTML

<input id="id_name" name="name"/>
<input id="id_age" name="age"/>
<button id="add_btn">click</button>

<table border="1" class="mytab"></table>    

JQuery:

$("#add_btn").on('click', function(e) {
    e.preventDefault();
    var name = $('#id_name').val(), 
        age = $('#id_age').val();

    $('<tr><td>'+name+'</td><td>'+age+'</td></tr>').appendTo( $('.mytab') );
});

DEMO

http://jsfiddle.net/jogesh_pi/dfg2R/

Helpful link: appendTo()

Sign up to request clarification or add additional context in comments.

1 Comment

Why no var before age? Edit: Nevermind, I didn't notice the comma.
0

A little search about jQuery DOM will give you the answer. see jQuery.append()

$( '#table_id' ).append(
    $('<tr />').append(
        $('<td/>').text($('#id_name').value())
    ).append(
        $('<td/>').text($('#id_age').value())
    )
);

Comments

0

You can use the insertRow and insertCell on the returned row.

http://jsfiddle.net/dfg2R/2/

insertCell

insertRow

Here is an example:

$("#myform").on('click', "#add_btn", function(e) {
        e.preventDefault();
        var table = $("table.mytab")[0]
        var newRow = table.insertRow(0);
        var newCell = newRow.insertCell();
        newCell.innerText = $("#id_name").val();
        var newCell = newRow.insertCell();
        newCell.innerText = $("#id_age").val();
});

Comments

0

If you want to use jQuery you could use .append()

$(".myTab tbody").append("<tr><td>" + valueName+ "</td><td> + valueAge + </td></tr>");

Comments

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.