Friday, May 17, 2024
 Popular · Latest · Hot · Upcoming
141
rated 0 times [  145] [ 4]  / answers: 1 / hits: 31697  / 12 Years ago, sun, march 11, 2012, 12:00:00

What is the best method in jQuery to add an additional row to a table as the first row?


I have a table like this


<table id="mytable" cellpadding="0" cellspacing="0">
<tr>
<td>col1</td>
<td>col2</td>
<td>col3</td>
</tr>
<tr>
<td>col1</td>
<td>col2</td>
<td>col3</td>
</tr>
</table>
<button id="but">mybutton</button>

I want to add a row as the first row to the beginning of the table with given default values. How can I accomplish this using JavaScript and jQuery? A fiddle will be helpful.


More From » jquery

 Answers
14

http://jsfiddle.net/32Ymw/



$(#but).click(function(){
row = $(<tr></tr>);
col1 = $(<td>col1</td>);
col2 = $(<td>col2</td>);
col3 = $(<td>col3</td>);
row.append(col1,col2,col3).prependTo(#mytable);
});​

[#86919] Friday, March 9, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
devinjadong

Total Points: 711
Total Questions: 117
Total Answers: 100

Location: Andorra
Member since Sat, May 27, 2023
1 Year ago
devinjadong questions
Thu, Feb 17, 22, 00:00, 2 Years ago
Wed, Dec 8, 21, 00:00, 2 Years ago
Tue, Oct 27, 20, 00:00, 4 Years ago
Fri, Oct 18, 19, 00:00, 5 Years ago
;