Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Feb 2012
    Location
    Orlando, FL
    Posts
    4
    Thanks
    3
    Thanked 0 Times in 0 Posts

    calling multiple functions help

    Hello, so I'm trying to call multiple functions with onClick. It doesnt really work, just wondering why. Advice or tips would be nice, thank you guys for your time. By the way this is a project for school so the comments are for my professor
    Code:
    <head>
    <title>Untitled Document</title>
    <script type="text/javascript">
    //hello, this code doesn't work, I did it piece by piece so the bits work, but when I combined them, it stopped working
    function texter(newText){
    	var oldHTML = document.getElementById('outputPrompt').innerHTML;
    	document.getElementById('outputPrompt').innerHTML = newText+"<br />"+oldHTML;
    	console.log(newText);
    }
    // texter function from class
    function menuMain(){
    	var userInput = document.getElementById('input').value;
    	var mainMenu;
    	if (userInput == "3"){
    		mainMenu = menuThree();
    	} else if (userInput== "2") {
    		mainMenu = menuTwo();
    	} else if (userInput== "1") {
    		mainMenu = menuOne();
    	}
    	texter(mainMenu)
    }
    //going to the functions by calling them				
    function menuOne(){
    		var userInput = document.getElementById('input').value; 
    	if (userInput.length == 0){
    		texter("You need to enter something in the input box");
    	} else   {
    		texter("the string has been saved");
    	} 
    	
    }
    // if the length is equal to zero then there was nothing inputted 
    function menuTwo() {
      var userInput = document.getElementById('input').value;
      var theArray = userInput.split(/\s|\n|\r/);
      //split string into words array i looked up the regex on coding forums
      //also had help to create this from coding forums
      var WordsFound = [i];
      for (var i=0; i<theArray.length-0; i++) {
        if (theArray[i].length < 5) { WordsFound.push('eep- '+theArray[i]); }
        else { if (theArray[i].length > 5) { WordsFound.push(theArrray[i]+' -zzz'); }
               else { WordsFound.push(theArray[i]); }
             }
      }
      document.getElementById('outputPrompt').innerHTML = WordsFound.join('<br>');
    }
    // if the array length is less than five the word will push it onto the array
    // vise versa with the array length being more than five
    //then i join the word with the array 
    function menuThree() {
    var userInput = document.getElementById('outputPrompt').reset();
    }
    //reset the output 
    function combineFunctions() {
        menuMain();
        menuOne();
        menuTwo(); 
        menuThree();
    }
    // combining the functions into one function so i can call it on onclick 	
    function cleartext(){
    document.getElementById('input').value="";
    }
    </script>
    </head>
    
    <body>
    <h1>RoboSpeek</h1>
    <textarea type='text' id='input' onfocus="cleartext()" cols="40"rows="10" wrap="physical">
    Please enter 1,2,3 or Exit</textarea><br />
    <input type='button' onclick='combineFunctions()' value='submit'/>
    <p id='outputPrompt'>Please enter 1,2,3 or exit only</b> </p> 
    </body>
    </html>
    Last edited by wcarbo; 02-25-2012 at 01:05 AM. Reason: different code

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,650
    Thanks
    80
    Thanked 4,638 Times in 4,600 Posts
    Makes no sense.

    In your menuMain function, you call one of menuOne or menuTwo or menuThree, depending on the user input.

    Then, in your combineFunctions function, you first call menuMain and then call *ALL THREE* of the other functions, no matter what the user input was.

    What was the point of carefully choosing the right one to call if you are then going to go ahead and call all three anyway?

    On top of that, the menuMain is expecting *ONLY* a number input of 1 or 2 or 3 yet, for example, menuTwo wants to split that *SAME* INPUT VALUE into words. If the value was only 1 or 2 or 3, there won't *BE* any words to split it into.

    Have you really thought through what you are trying to accomplish??? I am 90% certain you need *TWO DIFFERENT* input fields. One where you will enter 1 or 2 or 3 and the other where you will input words.

    But better than that would be to use 3 different *BUTTONS*, each marked to do a different task.

    I think you need to step back many paces and look at this from a distance. You are badly confused.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • Users who have thanked Old Pedant for this post:

    wcarbo (02-25-2012)

  • #3
    New to the CF scene
    Join Date
    Feb 2012
    Location
    Orlando, FL
    Posts
    4
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Hmm to be honest I have no idea what I'm doing (obviously).
    I really kind of gave up on it, I already turned it, just wondering if maybe there was a simple explanation on why it wasn't working or if I just did it completely wrong. Thank you for responding anyways though !

    Parameters for my project:
    My professor wants an input box, submit and output.
    First I need to have main menu where it tells the user to enter 1,2,3 or exit
    When you type in 1, it will tell you to enter a string. If the user enters a string it will say 'string saved' and go back to the main menu. If it doesn't it says 'no string'.
    Then when you type in 2 the previous string that was entered will be converted and the output will say 'string converted' and then return to the main menu.
    Then 3 should actually put the converted string into the output and then go back to the main menu.
    exit should clear everything from the output.

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,650
    Thanks
    80
    Thanked 4,638 Times in 4,600 Posts
    it will tell you to enter a string
    And *THAT* is the part you missed. No place in there, for number 1, do you then ask the user for another string. So the ONLY input field you have is the one where you entered 1,2,3. And so you never give the person a chance to enter that string.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  

    Posting Permissions

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