dcsimg
www.webdeveloper.com
Results 1 to 2 of 2

Thread: Problem with a do while loop.

  1. #1
    Join Date
    Feb 2013
    Posts
    19

    Problem with a do while loop.

    I'm learning javascript and Im working on a text game. The do while loop isnt working. It screws that page up. I guess it loops to much and it says theres a problem with the script on this page. I need to make the page wait until it gets an answer from the form before it moves on to the next function. How do I do it? Heres the code.

    <html>

    <head>
    <title> Legend of the Dragonstone </title>


    <head>

    <body>
    <header>

    <script src = "http://ajax.googleapis.com/ajax/libs/jquery/1.6.3/jquery.min.js"> </script>

    <script>



    $(document).ready(function() {


    function choosename() {
    var name = "";
    $("header").append("<p>Please enter your characters name.</p>");

    // This is the problem loop.

    do {
    $("#button").click(function(){
    $("p").hide();
    var name = $("#text").val();
    });
    } while (name == "");
    $("header").append("Welcome " + name +".");


    };

    function tarien() {
    $("header").append("<p>City of Tarien</p>");
    };


    choosename();
    tarien();

    });

    </script>

    </header>

    <body>

    <input type="text" id="text">
    <p></p>
    <input type="button" value="ok" id = "button">

    </body>

    </html>

  2. #2
    Join Date
    Dec 2012
    Posts
    170
    The while loop makes no sense, for every iteration of the loop you're adding a new event listener to the button listening for a click, you only need to do that once then check in the event function to see if the value is blank or not. You don't need, and shouldn't be using in this case, a while loop.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles