Question

I'm sure I am missing something really basic here, but its had me stuck for about an hour and I can't seem to fix the problem.

Basically, I'm attempting to create a method within an object that will check against a checkbox value in html, and if true display three values within the same object to the console (and eventually I'll write this to the innerHTML of my form).

The following code simply writes "function()" to my console in firebug. Obviously, I'm trying to write firstName lastName
email. What simple thing am I missing here? Thanks in advance!

function process() {
'use strict';

var firstName = document.getElementById("firstName").value;
var lastName = document.getElementById("lastName").value;
var email = document.getElementById("email").value;
var permission = document.getElementById("permission").checked;

var contactInfo = {

    firstName: firstName,
    lastName: lastName,
    email: email,
    permission: permission,
    display: function() {
        var contactCard;
        if (this.permission == true) {
            contactCard = firstName + " " + lastName + "<br />" + email
        } else {
            contactCard = "Permission Denied."
        }
        return contactCard;
    }
};
console.log(contactInfo.display);
return false;
};
function init() {
'use strict';
document.getElementById('theForm').onsubmit = process;
};

window.onload = init;
Was it helpful?

Solution

console.log(contactInfo.display());

You need to call the function.

You currently just have console.log(contactInfo.display); display is a function and is not being executed (so console will actually print the result of contactInfo.display.toString()), that is why you see 'function' and not the return value 'contactCard'.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top