View Full Version : Output to content div using custom function showit

03-19-2012, 06:44 PM
Hi Guys, Before I get started explaining my problem I would like to make it clear that this is a university web scripting assignment so I will not be asking for answers just advice as im stuck.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
<html lang="en">
body{font-family: Arial,sans-serif; background-color: #86bfc9;}
#content{float:right; width: 75%;}
<script type = "text/javascript">
function showit(what,where)
var node = document.getElementById(where);
while( node.firstChild )
node.removeChild( node.firstChild );
node.appendChild( document.createTextNode(what) );
function hello()
alert("I'm a Computer Solutions Degree student,\n get me out of here");
function timestable()
str="you code times table here";
function formattedtimestable()
str="you code formatted times table here";
function alerttimestable()
str="you code alert times table here";
// you code here
<h1>JavaScript Exercises </h1>
<div id="content"> <div id = "Results"></div> </div>
<a href="#" OnClick = "hello()">Hello Alert</a> <br/><br/>
<a href="#" OnClick = "timestable()">Times Tables</a> <br/><br/>
<a href="#" OnClick = "alerttimestable()">Times table in an alert box</a>
<a href="#" OnClick = "formattedtimestable()">Times Tables in a Table</a>

I have been given this skeleton code and need to created the relevant times tables so a times table in a alert box, a times table in a table and a standard times table. Now the problem isn't creating the table as that's easy the problem is with the showit function. When I use the function it only prints the string once and nothing else so for example if I did

for(x = 0; x< 9; x++)
showit(x, "Result");

it will only show 8 not 1-2-3-4-5-6-7-8.

or if i did

showit("Danny", "Result");
showit("Joe", "Result");

it will only output Joe not Danny.

The content needs to go in the content div and I cant make it output more than one thing which of course is a problem as I need to output the times tables.

Sorry for the poor explanation.

Hope someone can shed some light on this
Many Thanks

03-19-2012, 07:00 PM
showit is removing every
thing before adding the
latest thing.

03-19-2012, 07:06 PM
did you write this code? because it is doing exactly what you are telling it to, but you are saying you want it to do the opposite.

have a look at this bit:

while( node.firstChild )
node.removeChild( node.firstChild );
node.appendChild( document.createTextNode(what) );

in plain English: if the node where you are showing your results has a child, remove it, then create another.

how could it ever do anything but show the result of the last code fed to the function and wipe out all that has come before it in the process?

03-19-2012, 07:09 PM
Thanks for the fast responses, no i didnt write that skeleton code it is what we have been provided with and have to use. So with this code is it even possible to do what I need it to do?

03-19-2012, 07:21 PM
assuming that the remove child line is there for a reason (so that your results div gets cleared because showit can be called to do various things), it would seem that the best way would be to create the content before passing it to the function as a single argument.

Which doesn't seem to be a very good way to do it at all, but if that's what the assignment asks for, I guess it might look something like this...

y="" //create empty string
for(x = 0; x< 9; x++){
y+=x //adds each iterated value of x to string as loop progresses
showit(y, "Result"); //once loop is finished, feed entire string to function as argument

03-19-2012, 08:17 PM
right I have that bit working and its all good :thumbsup: thanks very much! the last problem I have is parsing html code such as <br /> or <td> through as in the function show it you create a text node so the html code gets turned to text! How would I get around this?

03-19-2012, 08:51 PM
in the function show it you create a text node so the html code gets turned to text

yeah, that's going to make it tricky