944,179 Members | Top Members by Rank

Ad:
Jan 26th, 2007
0

using javascript to dynamically add to existing html table

Expand Post »
I have been working on a questionnaire results page which takes the values from the questionaire on the previous page, calculates a score then plots their results on a grid. this all works fine, what i am a little baffled about however is, how do i dynamically add table rows/cells to an already existing table? if i use document.print() it just overwrites all the existing html etc.

here is the function i currently have to output the information:

function summary(){

var resultArr = new Array();
for(i=0;i<14;i++){
if(newValArr[i]==100){
resultArr[i] = "Strongly Agree";
}
if(newValArr[i]==75){
resultArr[i] = "Agree";
}
else if(newValArr[i]==50){
resultArr[i] = "Disagree"
}
else if(newValArr[i]==25){
resultArr[i] = "Strongly Disagree"
}
else if(newValArr[i]==0){
resultArr[i] = "Not my job";
}
if(i==13){
//alert('broken out of strongly agree loop');
}

}
for(i=14;i<27;i++){
if(newValArr[i]==100){
resultArr[i] = "Always";
}
if(newValArr[i]==75){
resultArr[i] = "Usually";
}
else if(newValArr[i]==50){
resultArr[i] = "Occasionally"
}
else if(newValArr[i]==25){
resultArr[i] = "Never"
}
else if(newValArr[i]==0){
resultArr[i] = "Not my job";
}
if(i==26){
//alert('broken out of always loop');
}
}


for(p=0;p<14;p++){
if(resultArr[p]=="Agree"){
document.write('<tr><td colspan="2">');
document.write('<p>'+(p+1)+'. You answered: '+resultArr[p]+'</p>');
document.write('</td></tr>');

}
if(resultArr[p]=="Disagree"){
document.write('<tr><td colspan="2">');
document.write('<p>'+(p+1)+'. You answered: '+resultArr[p]+'</p>');
document.write('</td></tr>');
}
if(resultArr[p]=="Strongly Disagree"){
document.write('<tr><td colspan="2">');
document.write('<p>'+(p+1)+'. You answered: '+resultArr[p]+'</p>');
document.write('</td></tr>');
}
if(resultArr[p]=="Not my job"){
document.write('<tr><td colspan="2">');
document.write('<p>'+(p+1)+'. You answered: '+resultArr[p]+'</p>');
document.write('</td></tr>');
}
if(p==13){
//alert('broken out of summary table creation loop 1');
}

}
for(p=14;p<27;p++){
if(resultArr[p]=="Usually"){
document.write('<tr><td colspan="2">');
document.write('<p>'+(p+1)+'. You answered: '+resultArr[p]+'</p>');
document.write('</td></tr>');
}
if(resultArr[p]=="Occasionally"){
document.write('<tr><td colspan="2">');
document.write('<p>'+(p+1)+'. You answered: '+resultArr[p]+'</p>');
document.write('</td></tr>');
}
if(resultArr[p]=="Never"){
document.write('<tr><td colspan="2">');
document.write('<p>'+(p+1)+'. You answered: '+resultArr[p]+'</p>');
document.write('</td></tr>');
}
if(resultArr[p]=="Not my job"){
document.write('<tr><td colspan="2">');
document.write('<p>'+(p+1)+'. You answered: '+resultArr[p]+'</p>');
document.write('</td></tr>');
}
if(p==26){
//alert('broken out of summary table creation loop 2');
}

}

until this morning i had the entire page being outputted using the document.write function, but after reading a response to another post i made, i took all the print()'s away and split the js up into several functions. everything works fine on my page, apart from this section (which is used to print a list of answers a user gives to the questionnaire)

I have a main table and i want to basically get this function to write html to append specific table rows/cells without over writing the rest of the html.

Any help would be much appreciated!

Thanks,

Mike
Reputation Points: 10
Solved Threads: 0
Newbie Poster
mikesowerbutts is offline Offline
24 posts
since Jan 2007
Jan 26th, 2007
0

Re: using javascript to dynamically add to existing html table

Could you please wrap your code with [code] tags the next time you post it?
Last edited by aniseed; Jan 26th, 2007 at 6:19 am.
Reputation Points: 48
Solved Threads: 7
Posting Whiz
aniseed is offline Offline
353 posts
since Apr 2006
Jan 26th, 2007
0

Re: using javascript to dynamically add to existing html table

ok i will do. im still stuck though!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
mikesowerbutts is offline Offline
24 posts
since Jan 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in JavaScript / DHTML / AJAX Forum Timeline: browser incompatibility? page wont finish loading
Next Thread in JavaScript / DHTML / AJAX Forum Timeline: Html Woes





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC