Geoff Cox <> writes:
> I am trying to print out the array values for a second time but get
> error on page message?
What error message do you get? (If you use IE, you should enable
error messages when doing development).
> <SCRIPT language="JavaScript">
Should be <script type="text/javascript">
> <!--
Not necessary.
> function display_questions()
> {
> var questions= new Array(5)
Here "questions" is declared as a local variable inside the
"display_questions" function. The "questions" variable is only
visible inside this function.
> document.write("<input type='button' value='again' onclick='again()' /
>>");
You seem to be trying to document.write an XHTML element (the closing
"/>" looks like XHTML), but document.write generally doesn't work
for XHTML pages parsed as such.
Anyway, when this button is clicked, the again function is called.
> function again()
> {
> var i=0;
> for (i = 0; i<5; i++)
A little shorter:
for (var i = 0; i < 5; i++)
> {
> document.write(this.questions[i] + "<br>");
This is called through a user click on a button. That suggests that the
page is already done loading when this is called. Doing "document.write"
on a page after it has finished loading will erase the entire page, and
replace it with what is written.
Also, the "this" operator refers to the global object (aka "window"),
which doesn't have a "questions" property.
> <input type="button" value="see questions"
> onclick="display_questions()" />
And this calls the first function, which also erases the page.
Generally, document.write is not the way to add content to a page
that is already loaded. There are different ways to do that, either
through the W3C DOM or using the proprietary "innerHTML" property.
<URL:http://jibbering.com/faq/#FAQ4_15>
/L
--
Lasse Reichstein Nielsen -
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'