Solved

insert document.write into a table column

Posted on 2001-07-10
5
374 Views
Last Modified: 2010-05-18
The following code creates a table and uses document.write to fill the cells. How can I get the entire output to go into an existing table's column, the td with the id="gradeCELL" ?


<html>
<body>
<script LANGUAGE="JavaScript">
v1=1
v2=2
 document.write('<TABLE BORDER=1 WIDTH=10><TR></TR><TR><TD COLSPAN=3 ALIGN=CENTER><B>'
 +"Grade Report"+'</B><BR></TD></TR>');
 
 document.write('<TR><TD><B>'+ "w" +'</B></TD> <TD><IMG SRC="d.gif" HEIGHT=10 WIDTH='+(v1/(v1+v2)*50)
 +' BORDER=1></TD><TD>'+v1+'</TD></TR>'
 +'<TR><TD><B>'+ "r"+'</B></TD><TD><IMG SRC="c.gif" HEIGHT=10 WIDTH='+(v2/(v1+v2)*50)
 +' BORDER=1></TD><TD>'+v2+'</TD></TR>'+'<TR><TD><B>'+'</TD></TR>');
 
 document.write('<TR><TD COLSPAN=3 ALIGN=CENTER><B>'+"pct="+ Math.round(10*(v2/(v1+v2)*100))/10.0 +"%"
+'</B></TD></TR></TABLE>');
</script>



<div align="left">
  <table border="1" width="100%">
    <tr>
      <td id="gradeCELL" width="100%">&nbsp;</td>
    </tr>
  </table>
</div>



</body>
</html>
0
Comment
Question by:mathprof
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 46

Expert Comment

by:fritz_the_blank
ID: 6271230
You can nest tables as follows:

<Table>
     <tr>
         <td>
             Cell 1
         </td>
         <td>
             <table>
                <tr>
                   <td>
                       Imbedded cell 1
                   </td>
                   <td>
                       Imbedded cell 2
                   </td>
                </tr>
              </table>
          </td>
      </tr>
</table>

Fritz the Blank
0
 
LVL 46

Expert Comment

by:fritz_the_blank
ID: 6271241
Using your examples above, I've embedded the table into the cell that you requested:

<html>
<body>
<div align="left">
 <table border="1" width="100%">
   <tr>
     <td id="gradeCELL" width="100%">

<script LANGUAGE="JavaScript">
v1=1
v2=2
document.write('<TABLE BORDER=1 WIDTH=10><TR></TR><TR><TD COLSPAN=3 ALIGN=CENTER><B>'
+"Grade Report"+'</B><BR></TD></TR>');

document.write('<TR><TD><B>'+ "w" +'</B></TD> <TD><IMG SRC="d.gif" HEIGHT=10 WIDTH='+(v1/(v1+v2)*50)
+' BORDER=1></TD><TD>'+v1+'</TD></TR>'
+'<TR><TD><B>'+ "r"+'</B></TD><TD><IMG SRC="c.gif" HEIGHT=10 WIDTH='+(v2/(v1+v2)*50)
+' BORDER=1></TD><TD>'+v2+'</TD></TR>'+'<TR><TD><B>'+'</TD></TR>');

document.write('<TR><TD COLSPAN=3 ALIGN=CENTER><B>'+"pct="+ Math.round(10*(v2/(v1+v2)*100))/10.0 +"%"
+'</B></TD></TR></TABLE>');
</script>
;</td>
   </tr>
 </table>
</div>
</body>
</html>


Is this what you mean?

Fritz the Blank
0
 

Author Comment

by:mathprof
ID: 6271626
Almost.

What I want to do is be able to dynamically insert the 3x3 table into the other table.

So, I want to be able to set the values of v1 and v2, then call the javascript (as a function)and have it put the 3x3 table into the other table.

Say we call the javascript Graph and then set v1, v2, then call graph.

Your code did OK, but the values of v1 and v2 will change.

Thanks!
0
 
LVL 46

Accepted Solution

by:
fritz_the_blank earned 50 total points
ID: 6272996
Ok, how about the following:

Function MakeCells(v1,v2)
{
document.write('<TABLE BORDER=1 WIDTH=10><TR></TR><TR><TD COLSPAN=3 ALIGN=CENTER><B>'
+"Grade Report"+'</B><BR></TD></TR>');

document.write('<TR><TD><B>'+ "w" +'</B></TD> <TD><IMG SRC="d.gif" HEIGHT=10 WIDTH='+(v1/(v1+v2)*50)
+' BORDER=1></TD><TD>'+v1+'</TD></TR>'
+'<TR><TD><B>'+ "r"+'</B></TD><TD><IMG SRC="c.gif" HEIGHT=10 WIDTH='+(v2/(v1+v2)*50)
+' BORDER=1></TD><TD>'+v2+'</TD></TR>'+'<TR><TD><B>'+'</TD></TR>');

document.write('<TR><TD COLSPAN=3 ALIGN=CENTER><B>'+"pct="+ Math.round(10*(v2/(v1+v2)*100))/10.0 +"%"
+'</B></TD></TR></TABLE>');
</script>
}


<div align="left">
 <table border="1" width="100%">
   <tr>
     <td id="gradeCELL" width="100%">
         <SCRIPT LANGUAGE=javascript>
          <!--
          =MakeCells(4, 2)
          //-->
         </SCRIPT>
     </td>
   </tr>
 </table>
</div>

In the above example, I called the function with the values 4 and 2, but I could call the function with any numeric values.

Is that what you wanted?
0
 

Author Comment

by:mathprof
ID: 6274175
Thanks,
Prof Weissman
0

Featured Post

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article, we'll look how to sort an Array in JavaScript, including the more advanced techniques of sorting a collection of records either ascending or descending on two or more fields. Basic Sorting of Arrays First, let's look at the …
The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question