Solved

insert document.write into a table column

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
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…

816 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now