Page 1 of 2 12 LastLast
Results 1 to 10 of 17

Thread: I need help with my code

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

    Question I need help with my code

    So yeah in this code it wont get past the "menu" when i press the Play button can you guys help me?

    Code:
    <html>
    <body>
    <div id="mydiv">
    <script>
    function myInventory()
    {
    document.write('<pre>');
    document.writeln("A warm blanket");
    document.writeln('<button onClick="newWorld()">Back</button>');
    		{
    		document.getElementById('mydiv').innerHTML = '';
    		}
    }
    function newWorld()
    {
    document.write('<pre>');
    document.writeln("<h1>Hello and welcome to World of My Core!</h1>");
    document.writeln("What would you like to do?");
    document.writeln('<button onClick="myInventory()">Inventory</button>');
    		{
    		document.getElementById('mydiv').innerHTML = '';
    		}
    }
    </div>
    </script>
    
    <h1>Welcome to My Core</h1>
    <p>Start by choosing your name</p>
    
    <form action="">
    Username: <input type=" text" name="Username"><br>
    </form>
    <p>Then choose your gender</p>
    
    <form>
    <input type="radio" name="gender" value="Male">Male<br>
    <input type="radio" name="gender" value="Female">Female
    </form>
    
    <p>Press the Play button to play!<p>
    
    <button onClick="newWorld()">Play</button>
    </body>
    </html>

  2. #2
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 517 Times in 503 Posts
    Blog Entries
    5

    Default

    You have a </div> tag inside your script.

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

    Default

    Quote Originally Posted by traq View Post
    You have a </div> tag inside your script.
    Now it wors but not how i liked it to work. Run the code in your browser and check my problem

  4. #4
    Join Date
    Sep 2007
    Location
    The Netherlands
    Posts
    1,313
    Thanks
    28
    Thanked 136 Times in 131 Posts
    Blog Entries
    29

    Default

    Using document.write in a function that executes after page load destroys the page (including lines for functions). That's why your functions don't work.
    I don't know exactly what you want. I made the following, which may be what you have in mind:
    <!doctype html>
    <html >
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
    <title>&nbsp;</title>

    <script>
    function myInventory()
    {
    document.body.innerHTML="<pre>A warm blanket<button onClick='newWorld()'>Back<\/button><\/pre>"
    }
    function newWorld()
    {
    document.body.innerHTML="<pre><h1>Hello and welcome to World of My Core!<\/h1>What would you like to do?<button onClick='myInventory()'>Inventory<\/button><\/pre>"
    }
    </script>

    <style>
    pre{font-family: arial; font-size: 12px}
    </style>
    </head>

    <body>
    <h1>Welcome to My Core</h1>
    <p>Start by choosing your name</p>

    <form >
    Username: <input name="Username"><br>
    </form>
    <p>Then choose your gender</p>

    <form>
    <input type="radio" name="gender" value="Male">Male<br>
    <input type="radio" name="gender" value="Female">Female
    </form>

    <p>Press the Play button to play!<p>

    <button onClick="newWorld()">Play</button>
    </body>
    </html>

  5. The Following User Says Thank You to molendijk For This Useful Post:

    NitroDev (10-23-2013)

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

    Default

    Arie You are my hero now!! Thanks alot dude your the best

    What about when i want a new button or a line do i use the document.body.innerHTML again?
    Last edited by NitroDev; 10-23-2013 at 01:30 PM.

  7. #6
    Join Date
    Sep 2007
    Location
    The Netherlands
    Posts
    1,313
    Thanks
    28
    Thanked 136 Times in 131 Posts
    Blog Entries
    29

    Default

    Quote Originally Posted by NitroDev View Post
    What about when i want a new button or a line do i use the document.body.innerHTML again?
    Yes, but it may depend on what you want to add.

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

    Default

    well i want to add a button to the next line of the code like in here

    Code:
    <!doctype html>
    <html >
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
    <title>&nbsp;</title>
    
    <script>
    function craft()
    {
    document.body.innerHTML"<pre><p>What do you want to craft?<\/p><\/pre>"
    }
    function explore()
    {
    document.body.innerHTML="<pre><h1>Where do you want to explore?<\/h1><button onClick='newWorld()'>Back<\/button><\/pre>"
    }
    function myInventory()
    {
    document.body.innerHTML="<pre><h1>This is your inventory"
    document.body.innerHTML="<pre>A warm blanket<button onClick='newWorld()'>Back<\/button><\/pre>"
    }
    function newWorld()
    {
    document.body.innerHTML="<pre><h1>Hello and welcome to World of My Core!<\/h1>What would you like to do?<button onClick='myInventory()'>Inventory<\/button><button onClick='explore()'>Explore<\/button><\/pre>"
    document.body.innerHTML="<pre><button onClick='craft()'>Craft<\/button><\/pre>"
    }
    </script>
    
    <style>
    pre{font-family: arial; font-size: 12px}
    </style>
    </head>
    
    <body>
    <h1>Welcome to My Core</h1>
    <p>Start by choosing your name</p>
    
    <form >
    Username: <input name="Username"><br>
    </form>
    <p>Then choose your gender</p>
    
    <form>
    <input type="radio" name="gender" value="Male">Male<br>
    <input type="radio" name="gender" value="Female">Female
    </form>
    
    <p>Press the Play button to play!<p>
    
    <button onClick="newWorld()">Play</button>
    </body>
    </html>

  9. #8
    Join Date
    Sep 2007
    Location
    The Netherlands
    Posts
    1,313
    Thanks
    28
    Thanked 136 Times in 131 Posts
    Blog Entries
    29

    Default

    You mean this?
    <!doctype html>
    <html >
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
    <title>&nbsp;</title>

    <script>
    function craft()
    {
    document.body.innerHTML="<pre><p>What do you want to craft?<\/p><\/pre>"
    }
    function explore()
    {
    document.body.innerHTML="<pre><h1>Where do you want to explore?<\/h1><button onClick='newWorld()'>Back<\/button><\/pre>"
    }




    function myInventory()
    {
    document.body.innerHTML="<pre>A warm blanket<button onClick='newWorld()'>Back<\/button><\/pre>"
    }
    function newWorld()
    {
    document.body.innerHTML="<pre><h1>Hello and welcome to World of My Core!<\/h1>What would you like to do?<button onClick='myInventory()'>Inventory<\/button><button onClick='craft()'>Craft<\/button><\/pre>"

    }
    </script>

    <style>
    pre{font-family: arial; font-size: 12px}
    </style>
    </head>

    <body>
    <h1>Welcome to My Core</h1>
    <p>Start by choosing your name</p>

    <form >
    Username: <input name="Username"><br>
    </form>
    <p>Then choose your gender</p>

    <form>
    <input type="radio" name="gender" value="Male">Male<br>
    <input type="radio" name="gender" value="Female">Female
    </form>

    <p>Press the Play button to play!<p>

    <button onClick="newWorld()">Play</button>
    </body>
    </html>

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

    Default

    well yeah but in a different line than the inventory button

  11. #10
    Join Date
    Sep 2007
    Location
    The Netherlands
    Posts
    1,313
    Thanks
    28
    Thanked 136 Times in 131 Posts
    Blog Entries
    29

    Default

    Like this?
    <!doctype html>
    <html >
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
    <title>&nbsp;</title>

    <script>
    function craft()
    {
    document.body.innerHTML="<pre><p>What do you want to craft?<\/p><button onClick='newWorld()'>Back<\/button><\/pre>"
    }
    function explore()
    {
    document.body.innerHTML="<pre><h1>Where do you want to explore?<\/h1><button onClick='newWorld()'>Back<\/button><\/pre>"
    }




    function myInventory()
    {
    document.body.innerHTML="<pre>A warm blanket<button onClick='newWorld()'>Back<\/button><\/pre>"
    }
    function newWorld()
    {
    document.body.innerHTML="<pre><h1>Hello and welcome to World of My Core!<\/h1>What would you like to do?<br><button onClick='myInventory()'>Inventory<\/button><br><button onClick='craft()'>Craft<\/button><\/pre>"

    }
    </script>

    <style>
    pre{font-family: arial; font-size: 12px}
    </style>
    </head>

    <body>
    <h1>Welcome to My Core</h1>
    <p>Start by choosing your name</p>

    <form >
    Username: <input name="Username"><br>
    </form>
    <p>Then choose your gender</p>

    <form>
    <input type="radio" name="gender" value="Male">Male<br>
    <input type="radio" name="gender" value="Female">Female
    </form>

    <p>Press the Play button to play!<p>

    <button onClick="newWorld()">Play</button>
    </body>
    </html>

  12. The Following User Says Thank You to molendijk For This Useful Post:

    NitroDev (10-24-2013)

Similar Threads

  1. Replies: 3
    Last Post: 05-12-2011, 03:43 AM
  2. Secret code / hidden message / obfuscated code - ???
    By newbie01.others in forum JavaScript
    Replies: 2
    Last Post: 01-14-2011, 10:29 AM
  3. Replies: 15
    Last Post: 06-11-2009, 12:27 PM
  4. Replies: 2
    Last Post: 10-27-2008, 05:16 AM
  5. Help - Code discrepency - Full Screen code
    By kurson in forum JavaScript
    Replies: 6
    Last Post: 05-10-2006, 06:53 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
  •