The same as other answers, in a jQuery fashion:
$.each(selectValues, function(key, value) {
$('#mySelect')
.append($("<option></option>")
.attr("value", key)
.text(value));
});
The approach you suggest is not guaranteed to give you the result you're looking for - what if you had a tbody
for example:
<table id="myTable">
<tbody>
<tr>...</tr>
<tr>...</tr>
</tbody>
</table>
You would end up with the following:
<table id="myTable">
<tbody>
<tr>...</tr>
<tr>...</tr>
</tbody>
<tr>...</tr>
</table>
I would therefore recommend this approach instead:
$('#myTable tr:last').after('<tr>...</tr><tr>...</tr>');
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):
$('#myTable > tbody:last-child').append('<tr>...</tr><tr>...</tr>');
Best Answer
I struggled with jQuery in Action because I felt it was more of a reference manual as opposed to a tutorial. Maybe a year ago it was the best reference/tutorial, but now there are a bunch of other alternatives.
On the other hand, I really enjoyed the book Javascript: The Missing Manual because I felt it had better examples. Don't let the title of the book fool you, after a quick introduction to JavaScript the author introduces you to jQuery and uses jQuery for the rest of the book.
I also purchased two PDF Books from Packt Publishing :
1) Learning jQuery 1.3
2) jQuery UI 1.6: The User Interface Library for jQuery
Both are recent publications and well written. You can often find "percentage off" discounts from your local user group or on the internet to apply to the purchase price of the downloadable PDF books.
If you're looking for a single book that covers both jQuery and the jQuery UI project, then this Wrox book might be useful: Beginning JavaScript and CSS Development with jQuery. A quick google search revealed that you can browse some of the contents of the book online at: Google Book Search - Beginning JavaScript Development With jQuery By Richard York
I would actually recommend learning from samples posted on the web. There are a bunch over at:
Learning jQuery - a site related to the Packt book listed above
Encosia - ASP.NET, AJAX and more
ASP.NET and MVC jQuery related posts at Haacked
Rick Strahl's Web Log