0

My existing table looks like this.

<table class="subheader" id="tableTelephone">
    <thead class="subheader">
        <tr>
            <td>
                @Resources.TYPE
            </td>
            <td>
                @Resources.TELEPHONE_NUMBER
            </td>
        </tr>
    </thead>
    <tbody class="subheader">
        @{foreach (Telephone phone in Model.TelephoneList)
        {
        <tr>
             <td>@phone.TelephoneType</td>
             <td>@phone.Number</td> 
        </tr>
        }
    }
    </tbody>
</table>

On a button click I want to fetch values from the text boxes & append to the above existing table. I am using below jquery for the same, but its not working, unless I append an empty TR after the foreach loop.

$('#tableTelephone > tr:last').after("<tr><td>" + type + "</td><td>" + number + "</td></tr>");

The empty is causing issues while retrieving data, so let me know if any solution is there to append the rows (without using a empty tr)

1
  • I guess the code block in foreach loop is not allowing to find last tr. Commented Aug 19, 2011 at 14:17

1 Answer 1

1
$('#tableTelephone').append(

"<tr><td>" + type + "</td><td>" + areaCode + "-" + number + "</td></tr>"
);

DEMO

or

$("<tr/>").append(
$("<td/>").text(type))
   .append($("<td/>").text(areaCode+"-"+number))
   .appendTo("#tableTelephone");

here is the fiddle http://jsfiddle.net/MecUs/2/

jquery .appendTo

Description:
Insert every element in the set of matched elements to the end of the target.
Sign up to request clarification or add additional context in comments.

3 Comments

It will append after the tbody of my table?
I tried with ur first option (.append()) & its not working. Its adding the elements, but in proper way i.e. outside the tbody.
Jsfiddle is indeed a great tool for such RnDs. Thanks.

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.