Timppa
Timppa

Reputation: 363

Call functions inside of another function

I am having a problem calling functions inside a function.

This is the sample code:

<script>
    function saveInfo() {
        function returnEmail() {
            var _e = document.getElementById("email").value;
            return _e;
        }

        function returnName() {
            var _n = document.getElementById("name").value;
            return _n;
        }
    }
</script>

The saveInfo() method is made in a button:

<input type="submit" value="Save" onclick="saveInfo()" style="color: black">

So there are 2 forms, where you fill up your email and name. By clicking "Save" -button, the DIV will disappear (this works) and another DIV will appear within text like this: Name = (name) | Email = (email).

I am having problems to call the saveInfo()'s returnEmail() for the corresponding line (where there is 'Name = ').

I try to write them like this:

<p>Email: 
   <script>
      var pEmail = saveInfo().returnEmail();
      document.write(pEmail);
    </script> <br>
</p>

I know that the script above is incorrect, this is not the only way I have tried to return it.

Upvotes: 1

Views: 1799

Answers (3)

rayepps
rayepps

Reputation: 2092

If your set on calling it like saveInfo().returnEmail(); then you can do the following. saveInfo returns an object containing the returnEmail method.

<script>
    function saveInfo() {
        // Do any desired logic
        return {
            returnEmail: function() {
                var _e = document.getElementById("email").value;
                return _e;
            },

            returnName: function() {
                var _n = document.getElementById("name").value;
                return _n;
            }
        }
    }
</script>

Upvotes: 1

Abhinav Galodha
Abhinav Galodha

Reputation: 9878

You need to return the exposed functions from the function saveInfo. At this time, your code only declares the function, but doesn't return anything. So, saveInfo returns undefined. Below approach is an implementation of the Revealing module pattern to reveal the public members outside your function.

function saveInfo() {
        var returnEmail = function () {
            var _e = document.getElementById("email").value;
            return _e;
        }

        var returnName= function () {
            var _n = document.getElementById("name").value;
            return _n;
        }

        return {
           returnEmail :returnEmail,
           returnName :returnName
        }
    }

Upvotes: 2

Carlos Rodriguez
Carlos Rodriguez

Reputation: 2220

It looks like you're trying to return those functions to use later. Try doing this instead. This function now returns an object with two functions.

function saveInfo() {
    return {
        returnEmail: function() {
            var _e = document.getElementById("email").value;
            return _e;
        },

        returnName: function() {
            var _n = document.getElementById("name").value;
            return _n;
        }
    }
}

Previously, your saveInfo function wasn't returning anything, so

saveInfo().returnEmail();

would evaluate to

undefined.returnEmail();

and you'd get an error

Upvotes: 4

Related Questions