My Javascript code displays a form to input a word. It passes to function checkWord via <form action="#" onsubmit="return checkWord();">. The function either confirms the word entered or says it's not in the data, which exists as words in an array. All display commands use document.write(). Problem: the confirmation or denial display wipes out the original word entry form and displays on a new page. How do I display it on the same page, just under the form where the word was submitted?
Thanks for your help.
Thanks, Fang. I tried, and used the links provided in your response, but I can't get anywhere. Problem is I don't understand the logic of creating a new element just to keep displaying on the original page. The code is below. Any help there is appreciated.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>DICTIONARY3</title>
<script type="text/javascript">
var nword="";
var okay=0;
var b=new Array();
var c=new Array();
var d=new Array();
var e="";
var end=new Array();
function readDictCookie()
{
c[0]="horse1dog1teeth1fork1gyroscope1";
}
function unpackDict()
{
for (i=0; i<20; i++)
{end[i]=c[i].indexOf("1");
b[i]=c[i].substring(0,end[i]);
c[i+1]=c[i].substring(end[i]+1,c[i].length);
}
}
var ele=null;
var msg=null;
function checkWord()
{
//FOLLOWING 4 LINES ADDED AFTER FANG'S SUGGESTION.
var ele=document.createElement("div");
ele="just before usermessage";
msg=document.getElementById("usermessage");
document.body.insertBefore(ele,msg);
if (document.forms[0].aword.value <= 0)
{alert("Please enter a word."); }
else
{nword = String(document.forms[0].aword.value); }
okay=0;
for (var i=0; i<20; i++)
{if (nword == b[i])
{okay=1;
document.write("word "+nword+" okay = "+okay);
}
}
if (okay==0)
{document.write("<b>"+nword+"</b> not in dictionary. Is <b>"+nword+"</b> a legitimate word? <br><br>");
document.write("<form onsubmit='return addWord();'><input type='radio' name='choose' value='Yes' />Yes <input type='radio' name='choose' value='No' />No <br><br>");
document.write("<input id='sb2' type='submit' value='Submit Answer' /></form>");
//FOLLOWING 1 LINE ADDED AFTER FANG'S SUGGESTION.
onclick=addWord();
}
}
</script>
</head>
<body>
<font face="Arial">
<h2>Dictionary Version 4</h2>
<script type = "text/javascript">
readDictCookie();
unpackDict();
</script>
Bookmarks