PDA

View Full Version : Textbox inside of a JS function?



NitroDev
10-18-2013, 10:42 AM
So yeah i really need it for my game in working on

Here's the function:


<script>
function newWorld()
{
document.write('<pre>');
document.writeln("Hello and welcome to World of My Core!");
document.writeln("What would you like to do?");
// textbox here
}
</script>

PS. i know im just a beginner at coding (coded for a month) so i kinda suck.

jscheuer1
10-18-2013, 12:11 PM
There is no textbox exactly. You need to choose either a text input or a textarea. Well you could devise an editable element to substitute for either of those, but let's not do anything extra just yet. A text input is one line, a textarea is multi line. A text input makes more sense here, so in simplest terms:


<script>
function newWorld()
{
document.write('<pre>');
document.writeln("Hello and welcome to World of My Core!");
document.writeln("What would you like to do?");
document.writeln('<input type="text">'); // textbox here
}
</script>

If you want a textarea:


<script>
function newWorld()
{
document.write('<pre>');
document.writeln("Hello and welcome to World of My Core!");
document.writeln("What would you like to do?");
document.writeln('<textarea cols="50" rows="5"></textarea>'); // textbox here
}
</script>

If you want either of those to line up with the question, you would use document.write, not writeln for the line that writes the text input/textarea.

I would advise against using document.write and document.writeln though because, if the page is already loaded, they will overwrite the page. For some things it's a quick and easy way to get something on the page while it's loading, and for beginners it can be a useful tool while learning. But generally it's better to create elements and/or text nodes and then insert them into the DOM either via document.createElement(), and document.CreateTextNode(), coupled with the appendChild() or insertBefore() mehods, or even using an existing element and changing or adding to its innerHTML property. Sometimes a combination works best. Creation and insertion are more complex. But since they avoid having to represent elements and possibly javascript as text strings, they are generally more reliable and easier to work with later.

But, as I said, if you're a beginner, document.write/writeln can familiarize you with javascript to a point.

NitroDev
10-18-2013, 12:27 PM
Okay thanks John that really helped me and now its working correctly:)