Adding a table row in jQuery
Stack How?
Stackorigin – The Community of Question and Answers Latest Articles
Read More About Python List Comprehension with examples
Python List Comprehension: Python List comprehension provides a much more short syntax for creating a new list based on the values of an existing list. Advantages of List Comprehension in Python: Time Efficient than loop Space Efficient than loop Needs ...
Do not accept an offer letter from companies.
Do not accept an offer letter from companies without verifying the following points. Designation Salary Details & CTC Distribution Leave Policies Working Hours & Days Medical Coverage/Insurance Probation Period Notice Period Benefits and Perks Bonus F&F Policy
How to call API using Ajax
AJAX stands for Asynchronous JavaScript and XML and its used for to make calls to the server to fetch some data. In this article, we will learn how to implement a simple API call using AJAX technology. How to use GET ...
The approach you suggest is not guaranteed to give you the result you’re looking for – what if you had a
tbody
for example:You would end up with the following:
I would therefore recommend this approach instead:
You can include anything within the
after()
method as long as it’s valid HTML, including multiple rows as per the example above.Update: Revisiting this answer following recent activity with this question. eyelidlessness makes a good comment that there will always be a
tbody
in the DOM; this is true, but only if there is at least one row. If you have no rows, there will be notbody
unless you have specified one yourself.DaRKoN_ suggests appending to the
tbody
rather than adding content after the lasttr
. This gets around the issue of having no rows, but still isn’t bulletproof as you could theoretically have multipletbody
elements and the row would get added to each of them.Weighing everything up, I’m not sure there is a single one-line solution that accounts for every single possible scenario. You will need to make sure the jQuery code tallies with your markup.
I think the safest solution is probably to ensure your
table
always includes at least onetbody
in your markup, even if it has no rows. On this basis, you can use the following which will work however many rows you have (and also account for multipletbody
elements):Try this : very simple way