This post looks at how to add a new option to a select with jQuery. Working Example. The following is an example showing an already populated select. Clicking the 'Add option' button adds a new option to the end of the select and makes it the selected one. JQuery Append Method. JQuery append method used to insert content to the end of each element inside matching of HTML elements. It inserts specified content as the last child of each element in the jQuery collection. With append method, the selector expression preceding the method is the container into which the content is inserted.
What is the best method in jQuery to add an additional row to a table as the last row?
Is this acceptable?
Are there limitations to what you can add to a table like this (such as inputs, selects, number of rows)?
Darryl HeinDarryl Hein
35 Answers
12 next
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 no tbody
unless you have specified one yourself.DaRKoN_ suggests appending to the
tbody
rather than adding content after the last tr
. This gets around the issue of having no rows, but still isn't bulletproof as you could theoretically have multiple tbody
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 one tbody
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 multiple tbody
elements):Community♦
Luke BennettLuke Bennett
jQuery has a built-in facility to manipulate DOM elements on the fly.
You can add anything to your table like this:
The
$('<some-tag>')
thing in jQuery is a tag object that can have several attr
attributes that can be set and get, as well as text
, which represents the text between the tag here: <tag>text</tag>
.![Jquery append html to div Jquery append html to div](/uploads/1/2/4/2/124268672/237222645.jpg)
This is some pretty weird indenting, but it's easier for you to see what's going on in this example.
NeilNeil
So things have changed ever since @Luke Bennett answered this question. Here is an update.
jQuery since version 1.4(?) automatically detects if the element you are trying to insert (using any of the
append()
, prepend()
, before()
, or after()
methods) is a <tr>
and inserts it into the first <tbody>
in your table or wraps it into a new <tbody>
if one doesn't exist. So yes your example code is acceptable and will work fine with jQuery 1.4+. ;)
Community♦
Salman AbbasSalman Abbas
What if you had a
<tbody>
and a <tfoot>
? Such as:
Then it would insert your new row in the footer - not to the body.
Hence the best solution is to include a
<tbody>
tag and use .append
, rather than .after
.ChadTChadT
I know you have asked for a jQuery method. I looked a lot and find that we can do it in a better way than using JavaScript directly by the following function.
index
is an integer that specifies the position of the row to insert (starts at 0). The value of -1 can also be used; which result in that the new row will be inserted at the last position.This parameter is required in Firefox and Opera, but it is optional in Internet Explorer, Chrome and Safari.
If this parameter is omitted,
insertRow()
inserts a new row at the last position in Internet Explorer and at the first position in Chrome and Safari.It will work for every acceptable structure of HTML table.
The following example will insert a row in last (-1 is used as index):
![Append On End Jquery Append On End Jquery](/uploads/1/2/4/2/124268672/548046082.png)
I hope it helps.
shashwatshashwat
I recommend
as opposed to
The
first
and last
keywords work on the first or last tag to be started, not closed. Therefore, this plays nicer with nested tables, if you don't want the nested table to be changed, but instead add to the overall table. At least, this is what I found.CurtorCurtor
In my opinion the fastest and clear way is
here is demo Fiddle
Also I can recommend a small function to make more html changes
If you use my string composer you can do this like
Here is demoFiddle
sulestsulest
This can be done easily using the 'last()' function of jQuery.
NischalNischal
I'm using this way when there is not any row in the table, as well as, each row is quite complicated.
style.css:
xxx.html
DominicDominic
For the best solution posted here, if there's a nested table on the last row, the new row will be added to the nested table instead of the main table. A quick solution (considering tables with/without tbody and tables with nested tables):
Usage example:
OshuaOshua
I was having some related issues, trying to insert a table row after the clicked row. All is fine except the .after() call does not work for the last row.
I landed up with a very untidy solution:
create the table as follows (id for each row):
etc ...
and then :
Avron OlshewskyAvron Olshewsky
I solved it this way.
Using jquery
Snippet
Anton vBRAnton vBR
You can use this great jQuery add table row function. It works great with tables that have
<tbody>
and that don't. Also it takes into the consideration the colspan of your last table row.Here is an example usage:
UzbekjonUzbekjon
Ricky GRicky G
Pankaj GargPankaj Garg
Neil's answer is by far the best one. However things get messy really fast. My suggestion would be to use variables to store elements and append them to the DOM hierarchy.
HTML
JAVASCRIPT
Community♦
GabrielOshiroGabrielOshiro
Jakir HossainJakir Hossain
I found this AddRow plugin quite useful for managing table rows. Though, Luke's solution would be the best fit if you just need to add a new row.
Community♦
Vitalii FedorenkoVitalii Fedorenko
Here is some hacketi hack code. I wanted to maintain a row template in an HTML page. Table rows 0...n are rendered at request time, and this example has one hardcoded row and a simplified template row. The template table is hidden, and the row tag must be within a valid table or browsers may drop it from the DOM tree. Adding a row uses counter+1 identifier, and the current value is maintained in the data attribute. It guarantees each row gets unique URL parameters.
I have run tests on Internet Explorer 8, Internet Explorer 9, Firefox, Chrome, Opera, Nokia Lumia 800, Nokia C7 (with Symbian 3), Android stock and Firefox beta browsers.
PS: I give all credits to the jQuery team; they deserve everything. JavaScript programming without jQuery - I don't even want think about that nightmare.
WhomeWhome
Vivek SVivek S
I Guess i have done in my project , here it is:
html
js
SNEH PANDYASNEH PANDYA
Daniel OrtegónDaniel Ortegón
As i have also got a way too add row at last or any specific place so i think i should also share this:
First find out the length or rows:
and then use below code to add your row:
Jaid07Jaid07
To add a good example on the topic, here is working solution if you need to add a row at specific position.
The extra row is added after the 5th row, or at the end of the table if there are less then 5 rows.
I put the content on a ready (hidden) container below the table ..so if you(or the designer) have to change it is not required to edit the JS.
d.raevd.raev
You can cache the footer variable and reduce access to DOM (Note: may be it will be better to use a fake row instead of footer).
Pavel ShkleinikPavel Shkleinik
if you have another variable you can access in
<td>
tag like that try.This way I hope it would be helpful
MEAbidMEAbid
will add a new row to the first
TBODY
of the table, without depending of any THEAD
or TFOOT
present.(I didn't find information from which version of jQuery .append()
this behavior is present.)You may try it in these examples:
In which example
a b
row is inserted after 1 2
, not after 3 4
in second example. If the table were empty, jQuery creates TBODY
for a new row.Alexey PavlovAlexey Pavlov
If you are using Datatable JQuery plugin you can try.
Refer Datatables fnAddData Datatables API
Praveena MPraveena M
vipul sorathiyavipul sorathiya
If you want to add
row
before the <tr>
first child.If you want to add
row
after the <tr>
last child.Sumon SarkerSumon Sarker
protected by Marc Gravell♦Jan 23 '11 at 10:07
Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).
Would you like to answer one of these unanswered questions instead?
Would you like to answer one of these unanswered questions instead?