Results 1 to 3 of 3

Thread: Textbox inside of a JS function?

  1. #1
    Join Date
    Oct 2013
    Location
    Finland
    Posts
    52
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Textbox inside of a JS function?

    So yeah i really need it for my game in working on

    Here's the function:

    Code:
    <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.
    Last edited by jscheuer1; 10-18-2013 at 11:51 AM. Reason: Format

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    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:

    Code:
    <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:

    Code:
    <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.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. The Following User Says Thank You to jscheuer1 For This Useful Post:

    NitroDev (10-18-2013)

  4. #3
    Join Date
    Oct 2013
    Location
    Finland
    Posts
    52
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default

    Okay thanks John that really helped me and now its working correctly

Similar Threads

  1. Putting Label inside textbox
    By letom in forum JavaScript
    Replies: 6
    Last Post: 04-29-2013, 07:07 PM
  2. setTimeout inside while loop does not function.
    By sniperman in forum JavaScript
    Replies: 3
    Last Post: 09-20-2009, 08:00 AM
  3. Replies: 9
    Last Post: 09-16-2008, 05:31 PM
  4. out side of function or inside of function
    By riptide in forum JavaScript
    Replies: 2
    Last Post: 07-12-2007, 04:10 PM
  5. Replies: 4
    Last Post: 06-17-2007, 08:40 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •