www.webdeveloper.com
Results 1 to 6 of 6

Thread: store prompt input in array, display array help.

Hybrid View

  1. #1
    Join Date
    Apr 2007
    Posts
    20

    store prompt input in array, display array help.

    This is what I'm trying to figure out. User inputs as many names as they want in a prompt until they input nothing. The names are stored in an array and displayed.

    ok, so I know how to display the stored names if I know how many prompts there are going to be, like this:


    var name= new Array(5);
    var y=0;
    for (y=0; y<5; y++)
    {
    name[y]=prompt('Enter a name!',' ');
    }
    var x=0;
    for (x=0; x<5; x++)
    {
    document.write(name[x] + "<br>");
    }

    And I know how to keep asking for prompt until the user enters nothing, like this:

    do
    {
    name = prompt("Enter some names");
    }
    while (name != ' ');

    Thats the problem, I can't figure out how to prompt and store since I have no idea how many prompts there will be and I dont know if I can combine these 2 codes or some other way to do it.
    Any help is apreciated.
    Last edited by jonblaze87; 04-30-2007 at 10:10 PM.

  2. #2
    Join Date
    May 2006
    Location
    Somewhere behind your screen
    Posts
    1,637
    is it what you want?

    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    		"http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="content-type" content="text/html; charset=iso-8859-1">
    <title>Store prompt input in array</title>
    <style type="text/css">
    <!--
    body{
    font-family:Verdana, Helvetica;
    font-size:12px;
    color:#000;
    background-color:#f8f8ff;
    }
    h2{
    position:absolute;
    top:20px;
    right:50px;
    padding-right:50px;
    font-size:18px;
    }
    .main{
    width:100%;
    text-align:center;
    padding-top:100px;
    }
    -->
    </style>
    <script language="javascript" type="text/javascript">
    <!--
    var givenNames = new Array();
    // this pattern matches to words and/or digits up from 1 char
    // anything else is ignored
    var pattern = /[\w\d]{1,}/ig;
    
    do{
    var name = prompt("Enter some names. Only letters and digits are accepted!\nEntering an empty field stops asking","");
    if(name && name.match(pattern)){givenNames.push(name);}
    }
    while(name != "");
    
    function displayNames(){
    if(givenNames.length > 0){
    document.getElementById("list").innerHTML = "<span style='color:Navy;font-weight:bold;'>Given names are:<\/span><br><br>" + givenNames.join("<br><br>");
    }
    else{document.getElementById("list").innerHTML = "<span style='color:Navy;font-weight:bold;'>Nothing has been given!<\/span>";}
    }
    //-->
    </script>
    </head>
    <body onload="setTimeout('displayNames()',500)">
    <h2>Store prompt input in array, then display it</h2>
    <div id="list" class="main"></div>
    </body>
    </html>
    xxx: Guess Buddhist riddle: "What is the sound of one hand clapping?"
    yyy: facepalm

  3. #3
    Join Date
    Dec 2005
    Location
    Queen Creek, AZ
    Posts
    1,564
    Does pushing the elevator button more than once make it arrive faster?
    Of course it does.....just like hitting a submit button multiple times makes the data process faster!



    //erlin!
    Design first! Code later!

  4. #4
    Join Date
    May 2006
    Location
    Somewhere behind your screen
    Posts
    1,637
    Lmao
    xxx: Guess Buddhist riddle: "What is the sound of one hand clapping?"
    yyy: facepalm

  5. #5
    Join Date
    Apr 2007
    Posts
    20
    thanks padonak

  6. #6
    Join Date
    Apr 2012
    Posts
    4
    how would you limiit the amount of entries from that code?

    and do you have to have the setTimeout() method?

    als how would you d this but with it having paragraphs when listed? as when i took the breaks out it didnt work
    thanks.

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