Solved

insert document.write into a table column

Posted on 2001-07-10
5
372 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
  • 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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…

685 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