1

I've tried everything, but my code doesn't work. I don't know why.

Here is it:

var table = $("#cpTableBody").html();  
$("#cpTableBody").html('');

var tmpContent = "";
for (i=0; i<cpData.length; i++) {
  var tmpTable = table;
  tmpTable.replace(/{firstname}/g, cpData[i].contact_person.firstname);
  tmpTable.replace(/{lastname}/g, cpData[i].contact_person.lastname);
  tmpContent += tmpTable;
}

$("#cpTableBody").html(tmpContent);
1
  • table never changes so var tmpTable = table; in the loop seems to be pointless reassignment, and that further implies that {firstname} and {lastname} are going to be replaced in the first loop iteration and all other iterations will do nothing. Am I missing something? Commented Apr 29, 2011 at 18:59

1 Answer 1

6

easy answer... you're not setting tmpTable = tmpTable.replace... like so:

var tmpContent = ""; 
for (i=0; i<cpData.length; i++) {   
    var tmpTable = table;   
    tmpTable = tmpTable.replace(/firstname/g, cpData[i].contact_person.firstname + "");   
    tmpTable = tmpTable.replace(/{lastname}/g, cpData[i].contact_person.lastname + "");   
    tmpContent += tmpTable; 
} 
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you so much :) 8 hours of coding make me a headache ;)
no problem. don't forget to mark this as the answer by clicking the checkmark on this post :) It will also help you when you ask more questions on this site because your "answer rate" will be 100% and encourage people to post.

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.