Note that there are some explanatory texts on larger screens.

plurals
  1. POWriting a javascript variable from user input to the document
    text
    copied!<p>This seems like it should be relatively straightforward, I cannot figure out what I'm doing wrong. I need to take input (user's name) from the user via textbox, set it as a variable, and display it on the page. I keep getting "undefined" instead of the user's name.</p> <p>Here's a condensed version. This will display a Div that covers the entire page which contains an input for the user to type their name. when the user hits submit the div disappears and the name is set to a variable. I left the form handling functions in case they are relevant to the problem.</p> <pre><code>&lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"&gt; &lt;HTML&gt; &lt;HEAD&gt; &lt;TITLE&gt;HOME&lt;/TITLE&gt; &lt;/HEAD&gt; &lt;BODY&gt; &lt;CENTER&gt; &lt;BR /&gt; &lt;FONT COLOR="RED" FACE="ARIAL BOLD" SIZE="5"&gt;test&lt;/FONT&gt; &lt;BR /&gt; &lt;script language="javascript"&gt; var currentTime = new Date(); var month = currentTime.getMonth() + 1; var day = currentTime.getDate(); var year = currentTime.getFullYear(); var name = uservariable; function setName(){ uservariable = document.forms["username"]["user"].value; var ele = document.getElementById("nameInput"); if(ele.style.display == "none") { ele.style.display = "block"; } else { ele.style.display = "none"; } document.getElementById('element_7').value = uservariable; } function validateForm() { var x=document.forms["username"]["user"].value; if (x==null || x=="") { alert("Rembrandt's first name was Beauregard, what is yours?"); } else { setName() } } &lt;/script&gt; &lt;DIV id="nameInput" STYLE="background-color:#ccc;position:fixed;width:100%;height:100%;top:0px;left:0px;z-index:1000;display:block;"&gt; &lt;br /&gt;&lt;br /&gt;&lt;FONT FACE="ARIAL BOLD" SIZE="3"&gt;First Name:&lt;/font&gt;&lt;br /&gt; &lt;form name="username" action="javascript:validateForm()"&gt; &lt;input type="text" name="user" value="" size="20"&gt;&lt;br&gt; &lt;input type="submit" value="Start Shift"&gt; &lt;/form&gt; &lt;/div&gt; &lt;script language="javascript"&gt; document.write(name + "s LEADS SUBMITTED FOR " + month + "/" + day + "/" + year + ":&lt;/B&gt;"); &lt;/script&gt; &lt;/center&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt; &lt;/div&gt; &lt;/CENTER&gt; &lt;/BODY&gt; &lt;/HTML&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload