views:

133

answers:

4

I have a empty table that I would like to populate based on data entered on the same page.

Example. User enter Name, DOB. Click Add. Table with Name and DOB column has +1 row. And additions can be made as many times as the user choose.

<table width= "100%">
        <tr>
            <th colspan="3">Owners Already Added</th>
        </tr>
        <tr>
            <td>(NAME)</td>
            <td>(DOB)</td>
        </tr>
</table>


<table id="Owner">
        <tr>
            <th colspan="2">Owner</th>
        </tr>
        <tr>
            <td>Name</td>
            <td><input id="txtName" type="text" /></td>
        </tr>
        <tr>
            <td>Address</td>
            <td><input id="txtDOB" type="text" /></td>
        </tr>
</table>
+2  A: 

In the first table, add an id of 'DisplayTable' and change your columns to have class="name" and class="dob" respectively.

The jquery to do this is as follows:

var $tr = $('#DisplayTable tr:last');
$tr.find('.name').text($('#txtName').val());
$tr.find('.dob').text($('#txtDOB').val());
Keith Rousseau
That code doesn't add row to table.
LukLed
That is what I would like to do. Is this answer a bit off, or should I go ahead and see what I can do?
You're right - the code doesn't add a new row. I took the assumption that we already had the row that we wanted to edit.
Keith Rousseau
Thank you for putting me in the correct direction... Thanks!!!
A: 

JQuery might be your friend here. See this question as it resembles what you are trying to do.

You might also need to do a post to the server to persist your data. See jQuery post for help

Rippo
+3  A: 

Just to complete Keith Rousseau's answer (added code to add new row):

<table id="displayTable">
    <tr>
        <td>Name</td>
        <td>Date of birth</td>
    </tr>
</table>
<table id="Owner">
    <tr>
        <th colspan="2">
            Owner
        </th>
    </tr>
    <tr>
        <td>
            Name
        </td>
        <td>
            <input id="txtName" type="text" />
        </td>
    </tr>
    <tr>
        <td>
            Date of birth
        </td>
        <td>
            <input id="txtDOB" type="text" />
        </td>
    </tr>
</table>
<button onclick="addNewRow();">Add</button>

<script type="text/javascript">
    function addNewRow() {
        $('#displayTable tr:last').after('<tr><td class="name"></td><td class="dob"></td></tr>');
        var $tr = $('#displayTable tr:last');
        $tr.find('.name').text($('#txtName').val());
        $tr.find('.dob').text($('#txtDOB').val());
    }
</script>
LukLed
YESS!!! Thank you!!!
A: 

Well, since it is jQuery, and a table of sorts, you might look into a plug-in such as jqGrid which we have used with sucess.

See the link here: jqGrid

They have examples doing what you describe either in-line in the table, or in a modal dialog, lots of options here.

Mark Schultheiss