www.webdeveloper.com
Results 1 to 6 of 6

Thread: Text not being displayed after calling a function. Easy problem, please help!

  1. #1
    Join Date
    Nov 2009
    Posts
    2

    Text not being displayed after calling a function. Easy problem, please help!

    I'm coding a simple random sentence generator & this is the beginning part of what I'm trying to accomplish.

    I created 3 different functions with 3 separate buttons & 3 separate text areas to display the noun, verb, or article once the button is pressed.

    However, when I press a button, nothing shows up in the text area! Can someone please tell me what I'm doing wrong & give me a fix? It has to be a simple problem, but I am very very new to this & need help. Thanks in advance!

    Code:
    <?xml version="1.0" encoding="ISO-8859-1"?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    
    
    <head>
    
    <script language="JavaScript">
    
        function noun()
        // Assumes: nothing
        // Returns: a random noun
        {
            return RandomOneOf(["man", "woman", "ball"]);
        }
    
    </script>
    
    
    <script language="JavaScript">
    
        function verb()
        // Assumes: nothing
        // Returns: a random noun
        {
            return RandomOneOf(["hit", "liked", "smelled"]);
        }
    
    </script>
    
    
    <script language="JavaScript">
    
        function article()
        // Assumes: nothing
        // Returns: a random noun
        {
            return RandomOneOf(["the", "a", "some"]);
        }
    
    </script>
    
    </head>
    
    <body>
    
    
    <input type="button" value="Noun"
               onclick="document.getElementById('OutNoun').value = noun();" />
    
    <TEXTAREA ID="OutNoun" ROWS=1 COLS=15 WRAP VALUE=""></TEXTAREA> 
    
    
    
    <input type="button" value="Verb"
               onclick="document.getElementById('OutVerb').value = verb();" />
    
    <TEXTAREA ID="OutVerb" ROWS=1 COLS=15 WRAP VALUE=""></TEXTAREA> 
    
    
    
    <input type="button" value="Article"
               onclick="document.getElementById('OutArticle').value = article();" />
    
    <TEXTAREA ID="OutArticle" ROWS=1 COLS=15 WRAP VALUE=""></TEXTAREA> 
    
    
    
    
    
    
    </body>
    </html>

  2. #2
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,410

    Exclamation Missing some code ...

    You can combine all three <script...> sections into one
    <script type="text/javascript">
    // your code
    </script>
    But I do not see where you have RandomOneOf() function defined.

    Probably failing because it cannot find the function.
    What kind of errors are you getting in error console if using FF
    or any kind of alert message using MSIE browser?

  3. #3
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,410

    Lightbulb Consider this ...

    Assuming I have added the missing function correctly...
    Try this:
    Code:
    <?xml version="1.0" encoding="ISO-8859-1"?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    
    <script type="text/javascript">
    // From: http://www.webdeveloper.com/forum/showthread.php?p=1045646#post1045646
    
    function RandomOneOf(arr) {  // alert(arr.join('\n'));
      var cnt = arr.length;
      var rnd = Math.floor(Math.random() * cnt);
      return arr[rnd];
    }
    
    function noun() {
    // Assumes: nothing
    // Returns: a random noun
      return RandomOneOf(["man", "woman", "ball"]);
    }
    
    function verb() {
    // Assumes: nothing
    // Returns: a random noun
      return RandomOneOf(["hit", "liked", "smelled"]);
    }
    
    function article() {
    // Assumes: nothing
    // Returns: a random noun
      return RandomOneOf(["the", "a", "some"]);
    }
    
    </script>
    </head>
    
    <body>
    <input type="button" value="Noun"
               onclick="document.getElementById('OutNoun').value = noun();" />
    <TEXTAREA ID="OutNoun" ROWS=1 COLS=15 WRAP VALUE=""></TEXTAREA> 
    <input type="button" value="Verb"
               onclick="document.getElementById('OutVerb').value = verb();" />
    <TEXTAREA ID="OutVerb" ROWS=1 COLS=15 WRAP VALUE=""></TEXTAREA> 
    <input type="button" value="Article"
               onclick="document.getElementById('OutArticle').value = article();" />
    <TEXTAREA ID="OutArticle" ROWS=1 COLS=15 WRAP VALUE=""></TEXTAREA> 
    
    </body>
    </html>

  4. #4
    Join Date
    Nov 2009
    Posts
    2
    Thank you for the replies! I am following a set of guidelines (its a project for school) & was supposed to link an external javascript file into the code. I feel pretty stupid, but your help was greatly appreciated. I also got rid of the extra <script> tags.

  5. #5
    Join Date
    Jan 2010
    Posts
    1
    If I wanted to create a function called "sentence" that called each of the three previous functions in order to generate a random sentence with the form "article"+"noun"+verb", how would I do that?

  6. #6
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,410

    Lightbulb

    Code:
    function sentence() {
      return article()+' '+noun()+' '+verb();
    }

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