tags:

views:

84

answers:

5

Hi I was trying to add a row to a table using jQuery, but it is not working. What might be the reason?

And, can i put in some value to the newly added row..?

Here is the code:

<html>
<head>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
 $('a').click(function() {
$('#myTable').childs('tr').append('<tr class="child"><td>blahblah<\/td></
tr>');
});
</script>
<title></title>
</head>
<body>
<a href="">Link</a>
<table id="myTable">
  <tbody>
    <tr>
      <td>
        test
      </td>
    </tr>
  </tbody>
</table>
</body>
</html>
A: 
 $('a').click(function() {
$('#myTable tr').append('<td>blahblah</td>');
});

try this

antpaw
This will add that cell to ALL `TR`'s. Not a good idea.
MDCore
A: 

You should append to the table and not the rows.

<script type="text/javascript">
$('a').click(function() {
    $('#myTable').append('<tr class="child"><td>blahblah<\/td></tr>');
});
</script>
rsilva
I tried the above, but did not work.. :(
Amit
ok, you are doing something else wrong, please check here (http://secretgeek.net/rtjqe/realtimejqueryeditor.htm) your own code with my solution or dominic solution you will see that works.
rsilva
+2  A: 

I'm assuming you want to add this row to the <tbody> element, and simply using append() on the <table> will insert the <tr> outside the <tbody>, with perhaps undesirable results.

$('a').click(function() {
   $('#myTable tbody').append('<tr class="child"><td>blahblah</td></tr>');
});

EDIT: Here is the complete source code, and it does indeed work: (Note the $(document).ready(function(){});, which was not present before.

<html>
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.1/jquery.min.js"&gt;&lt;/script&gt;
<script type="text/javascript">
$(document).ready(function() {
    $('a').click(function() {
       $('#myTable tbody').append('<tr class="child"><td>blahblah</td></tr>');
    });
});
</script>
<title></title>
</head>
<body>
<a href="javascript:void(0);">Link</a>
<table id="myTable">
  <tbody>
    <tr>
      <td>test</td>
    </tr>
  </tbody>
</table>
</body>
</html>
Dominic Barnes
Did not work.. :(
Amit
in my experience you cannot modify the html contents of a table. Try using document.createElement("tr"), and document.appendElement()
mrwayne
I modified your source so that it should work correctly. (Note the edit above) I didn't notice this before, but you didn't have the `$(document).ready()` included, which basically breaks everything. Unless you wait for the `ready()` event to fire, the DOM isn't loaded and your jQuery selector probably won't find what it is looking for.
Dominic Barnes
A: 

Here is the code that works

<html>
<head>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
function AddRow()
{
    $('#myTable').append('<tr><td>test 2</td></tr>')
}
</script>
<title></title>
</head>
<body>
<input type="button" id="btnAdd" onclick="AddRow()"/>
<a href="">test</a>
<table id="myTable">
  <tbody >
    <tr>
      <td>
        test
      </td>
    </tr>
  </tbody>
</table>
</body>
</html>
Amit
and following can be used too var value1 = 1; $('#myTable').append('<tr><td>' + value1+'</td></tr>')
Amit
A: 

try

$("#myTable").append("<tr><%= escape_javascript( render :partial => name_of_partial ) %></tr>");

And in the partial, you should hhave

<td>row1</td>
<td>row2</td>
punit