I have a problem adding the data array into my table. There is no error message shown in the firebug and the data was not added into the table as rows using the $("#tbNames tr:last").after("<tr><td>" + data[0] + "</td><td>" + data[2] + "</td><td><img src='delete.gif' class='delete' height='15' /></td></tr>");.
The Logic (Javascript)
<script type="text/javascript">
var data = [];
data.push("Coco", "Mandy");
data.push("Suzze", "Candy");
data.push("Janny", "Jacky");
$(document).ready(function() {
$('#btnAdd').live('click', function() {
var name = $('#txtName').val();
var name2 = $('#txtName2').val();
$("#tbNames tr:last").after("<tr><td>" + name + "</td><td>" + name2 + "</td><td><img src='delete.gif' class='delete' height='15' /></td></tr>");
});
$('#tbNames td img.delete').live('click', function() {
$(this).parent().parent().remove();
});
$("#insert_data").click(function() {
for(var i=0; i<data.length; i++){
$("#tbNames tr:last").after("<tr><td>" + data[0] + "</td><td>" + data[2] + "</td><td><img src='delete.gif' class='delete' height='15' /></td></tr>");
}
});
});
</script>
The HTML form
<input id="txtName" type="text" />
<input id="txtName2" type="text" />
<input id="btnAdd" type="button" value="Add" />
<table id="tbNames" border="1" >
<tr>
<th>Name</b></th>
<th>Name2</b></th>
<th>Delete</b></th>
</tr>
<tr>
<td>Bingo</td>
<td>Tingo</td>
<td><img src="Delete.gif" height="15" class="delete" /></td>
</tr>
</table>
<input id="insert_data" type="button" style="height: 35px; width: 225px" value="Retrieve Default User" />
Please advise if I miss out anything. Thanks.
The Solution
(Will be insert into the solution text area tomorrow since I got this message Users with less than 100 reputation can't answer their own question for 8 hours after asking. You may self-answer in 7 hours. Until then please use comments, or edit your question instead.
Bug 1
Changing the
data.push("Coco", "Mandy");
data.push("Suzze", "Candy");
data.push("Janny", "Jacky");
to
data.push(["Coco", "Mandy"]);
data.push(["Suzze", "Candy"]);
data.push(["Janny", "Jacky"]);
Bug 2
Changing the
$("#insert_data").click(function() {
for(var i=0; i<data.length; i++){
$("#tbNames tr:last").after("<tr><td>" + data[0] + "</td><td>" + data[2] + "</td><td><img src='delete.gif' class='delete' height='15' /></td></tr>");
}
});
to
$("#insert_data").click(function() {
for(var i=0; i<data.length; i++){
$("#tbNames tr:last").after("<tr><td>" + data[i][0] + "</td><td>" + data[i][1] + "</td><td><img src='delete.gif' class='delete' height='15' /></td></tr>");
}
});
tableand if I can extend the functionality of thetable