PDA

View Full Version : Resolved Write The Text



[Nicolas]
02-06-2012, 05:44 PM
Okay, I am having some troubles with this code, I want it to document.write what the user types into the text box then clicks the button.
Though, when I click the button it document.writes nothing.
Is there a problem?


<html>
<head>
<script>
function write()
{
document.write("<html><head><title>Hi</title></head><body>");
document.write(" " + txt + " ");
document.write("</body></html>
}
txt=document.getElementById("intro").value;
</script>
<body>

<input type="text" id="intro" value="Text" />
<button onclick="write()">Test</button>
</body>
</html>


I don't see anything wrong. I use Firefox.
Help please?

keyboard
02-06-2012, 11:07 PM
This should work -



<html>
<head>
<script type="text/javascript">
<!--
function writer() {
var txt = document.getElementById("intro").value;
document.write("<html><head><title>Hi</title></head><body>");
document.write(" " + txt + " ");
document.write("</body></html>");
}
//-->
</script>
</head>
<body>
<input type="text" value="bob" id="intro">
<button onclick="writer()">Test</button>
</body>
</html>


There were a couple of problems -


document.write("</body></html>

should look like this


document.write("</body></html>");


Also, you named your function write(). I don't think you can name a function write so I renamed it writer() and it worked fine in IE (May be different in Firefox...).

Keyboard1333

[Nicolas]
02-06-2012, 11:55 PM
Well, I had another problem.
Now, nothing will write whatsoever.


<html>
<head>
<script type="text/javascript">
<!--
function writer() {
var txt = document.getElementById("intro").value;
document.write("<html><head><title>Hi</title></head><body>");
document.write(" " + html.body + " ");
document.write("</body></html>");
}
//-->
</script>
<title>My Code Tester</title>
</head>
<body bgcolor="yellow">
<center>
<noscript>Sorry, you don't have Javascript enabled so this script won't work.</noscript>
<form name="html" id="html">
Title: <input type="text" id="title" name="title" style="background-color:red;color:white;" value="bob"><br>
Body: <textarea id="body" name="body" style="background-color:red;color:white;">bob</textarea><br><br>
<button onclick="writer()" style="border: 0px;color: yellow;background-color: blue;height:50px;width:300px;font-size:30px;">~.~.~.~.~-Try-~.~.~.~.~</button>
</form>
</body>
</html>


I once again see nothing wrong in the code, it just won't work. I know it will because I made a website creator out of JavaScript using this technique.
Please help.

Edit: Terribly sorry, I noticed my error. I forgot to make the variable. Thank you again!