I am trying to add a table row dynamically via jQuery. Everything works fine but the row just won’t add itself to the last < tr>. Instead, it adds itself above the < table>…
I am quite confused. Could anyone help me out? Here is the code
Javascript
var counterx = 2;
var counter = 2;
$(document).ready(function(){
$("#addMoreRcpt").click(function(){
if (counterx>10){
alert("Only 10 reciepients are allowed");
return false;
}
var newTextBoxDiv = $(document.createElement('div')).attr("id", 'RcptEml_' + counter);
newTextBoxDiv.append("<tr><td>New Data</td><td>New Data</td><td>New Data</td></tr>");
newTextBoxDiv.appendTo("#RcptGroup");
counter++;
counterx++;
});
});
function fncDelRcpt(id){
$("#RcptEml_"+id).remove();
counterx--;
}
HTML
<table border=1>
<div id="RcptGroup">
<tr>
<th>1</th>
<th>2</th>
<th>3</th>
</tr>
<div id="1">
<tr>
<td>data</td>
<td>Data</td>
<td>data</td>
</tr>
</div>
</div>
</table>
<br /><a id="addMoreRcpt" href="#" >Add more reciepients</a>
Here’s changed parts of your code that should work:
The Script:
The Table:
Working fiddle: http://jsfiddle.net/pratik136/2au73/5/
As suggested in the comments to your question,
<Div/>s cannot be nested within<Table/>s. Here’s a guideline forHTML 4.01, but almost all of it should still hold true: http://www.cs.tut.fi/~jkorpela/html/nesting.html