2

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>"); 
    } 
});
11
  • Now you are doing the reverse :) Commented Feb 3, 2012 at 5:59
  • @AmarPalsapure yep and a chance for you to increase your rep points too. :D Commented Feb 3, 2012 at 6:00
  • Is this homework you are doing? or some project? Commented Feb 3, 2012 at 6:02
  • It is neither homework nor project. Just trying out how I can play around with the table and if I can extend the functionality of the table Commented Feb 3, 2012 at 6:03
  • @AmarPalsapure I found the solution. There are 2 bugs in my code. Commented Feb 3, 2012 at 6:08

2 Answers 2

4

The data arrary contains: ['Coco','Mandy','Suzze','Candy','Janny','Jacky']

So, currently your code produces:

<tr><td>Coco</td><td>Suzze</td><td><img ...etc></td></tr>
<tr><td>Coco</td><td>Suzze</td><td><img ...etc></td></tr>
<tr><td>Coco</td><td>Suzze</td><td><img  ...etc

Did you mean to write:

$("#insert_data").click(function() {
  for(var i=0; i<data.length-1; i=i+2){
    $("#tbNames tr:last").after("<tr><td>" + data[i] + "</td><td>" + data[i+1] + "</td><td><img src='delete.gif' class='delete' height='15' /></td></tr>");
    }
});
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your suggestion, actually I notice that there is 2 bugs in my code. Now is working fine. Thanks.
2

Here is your solution. Spot the difference :).

$(document).ready(function() {
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[i++] 
                 + "</td><td>" + data[i] 
                 + "</td><td><img src='delete.gif' class='delete' height='15' /></td></tr>");
        }
      });
   });
});

3 Comments

Thanks for your suggestion, actually I notice that there is 2 bugs in my code. Now is working fine. Thanks.
Glad to know that, but missed answer by 3 mins :(
it's ok, I am still thinking what to do next to the table. probably you can give me some ideas.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.