View Full Version : calling multiple functions help

02-25-2012, 12:22 AM
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

<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;
// 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();
//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() {
// combining the functions into one function so i can call it on onclick
function cleartext(){

<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>

Old Pedant
02-25-2012, 01:30 AM
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.

02-25-2012, 01:44 AM
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.

Old Pedant
02-25-2012, 01:58 AM
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.