"Undefined" in text box

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • car6006
    New Member
    • Jun 2007
    • 2

    "Undefined" in text box

    Why am I getting the "undefined" in my txt box, I have tried everything! I am new here so I truly hope you are able to assist me...

    Thanks

    <html>
    <head>
    <title>Random Proverbs</title>
    <script type="text/javascript">
    <!-- HIDE FROM INCOMPATIBLE BROWSERS
    quotes = new Array( ); // Initiated NEW ARRAY and also Added ( ) brackets
    quotes[0] = "Laughter is the best medicine.";
    quotes[1] = "Never look a gift horse in the mouth.";
    quotes[2] = "One good turn deserves another.";
    quotes[3] = "The early bird catches the worm.";
    quotes[4] = "Two is company, three is a crowd.";

    function changeQuote() {
    var newQuote = quotes[Math.round(Math .random()+quote s.length)];
    document.quotef orm.quote.value = newQuote;
    }
    var tick = setInterval(cha ngeQuote, 1000); // Add interval value to change the quote.

    // STOP HIDING FROM INCOMPATIBLE BROWSERS -->
    </script>
    </head>
    <body>
    <form name="quoteform " action=""> // ADDED form name that was missing
    <input type="text" size="50" name="quote" /><br />
    </form>
    </body>
    </html>
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by car6006
    Why am I getting the "undefined" in my txt box, I have tried everything! I am new here so I truly hope you are able to assist me...

    Thanks

    <html>
    <head>
    <title>Random Proverbs</title>
    <script type="text/javascript">
    <!-- HIDE FROM INCOMPATIBLE BROWSERS
    quotes = new Array( ); // Initiated NEW ARRAY and also Added ( ) brackets
    quotes[0] = "Laughter is the best medicine.";
    quotes[1] = "Never look a gift horse in the mouth.";
    quotes[2] = "One good turn deserves another.";
    quotes[3] = "The early bird catches the worm.";
    quotes[4] = "Two is company, three is a crowd.";

    function changeQuote() {
    var newQuote = quotes[Math.round(Math .random()+quote s.length)];
    document.quotef orm.quote.value = newQuote;
    }
    var tick = setInterval(cha ngeQuote, 1000); // Add interval value to change the quote.

    // STOP HIDING FROM INCOMPATIBLE BROWSERS -->
    </script>
    </head>
    <body>
    <form name="quoteform " action=""> // ADDED form name that was missing
    <input type="text" size="50" name="quote" /><br />
    </form>
    </body>
    </html>
    Moved to Javascript forum.
    And please use code tags next time when posting code.

    Comment

    • dmjpro
      Top Contributor
      • Jan 2007
      • 2476

      #3
      Originally posted by car6006
      Why am I getting the "undefined" in my txt box, I have tried everything! I am new here so I truly hope you are able to assist me...

      Thanks

      <html>
      <head>
      <title>Random Proverbs</title>
      <script type="text/javascript">
      <!-- HIDE FROM INCOMPATIBLE BROWSERS
      quotes = new Array( ); // Initiated NEW ARRAY and also Added ( ) brackets
      quotes[0] = "Laughter is the best medicine.";
      quotes[1] = "Never look a gift horse in the mouth.";
      quotes[2] = "One good turn deserves another.";
      quotes[3] = "The early bird catches the worm.";
      quotes[4] = "Two is company, three is a crowd.";

      function changeQuote() {
      var newQuote = quotes[Math.round(Math .random()+quote s.length)];
      document.quotef orm.quote.value = newQuote;
      }
      var tick = setInterval(cha ngeQuote, 1000); // Add interval value to change the quote.

      // STOP HIDING FROM INCOMPATIBLE BROWSERS -->
      </script>
      </head>
      <body>
      <form name="quoteform " action=""> // ADDED form name that was missing
      <input type="text" size="50" name="quote" /><br />
      </form>
      </body>
      </html>

      Welcome to TSDN.

      check the value of Math.round(Math .random()+quote s.length) using ........

      [code=javascript]
      alert(Math.roun d(Math.random() +quotes.length) );
      [/code]

      KInd regards,
      Dmjpro.

      Comment

      • acoder
        Recognized Expert MVP
        • Nov 2006
        • 16032

        #4
        Changed thread title.

        quotes.length is 5 while the indices to the array are 0-based going from 0 to 4, so you will need to subtract 1.

        Comment

        Working...