Solved

insert document.write into a table column

Posted on 2001-07-10
5
364 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 Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

This article shows how to create and access 2-dimensional arrays in JavaScript.  It includes a tutorial in case you are just trying to "get your head wrapped around" the concept and we'll also look at some useful tips for more advanced programmers. …
Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
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…

746 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

11 Experts available now in Live!

Get 1:1 Help Now