0

I tried to insert a function into javascript object but I get

undefined

This is supposed to return a message into div.

I would like to do it the first way, but here is the both way I tried :

var errorMessage = {
     empty: function(message){"<div class='field'><div class='csv'><span class='icon'></span><label class='manual' id='error-message'>" + message + "</label></div></div>"
    }
 };

console.log(errorMessage.empty("Hello"));

I also try this way

function errorMessage(message){
    "<div class='field'><div class='csv'><span class='icon'></span><label class='manual' id='error-message'>" + message + "</label></div></div>"

}

console.log(errorMessage("hello"))

2
  • 1
    There is no return statement on the function. Commented Oct 14, 2016 at 13:03
  • What do you expect the function to do? It's literally just a string literal in a function. Commented Oct 14, 2016 at 13:03

2 Answers 2

6

You need a return of the value literally.

The return statement ends function execution and specifies a value to be returned to the function caller.

var errorMessage = {
        empty: function (message) {
            return "<div class='field'><div class='csv'><span class='icon'></span><label class='manual' id='error-message'>" + message + "</label></div></div>";
        }
    };

console.log(errorMessage.empty("Hello"));

Sign up to request clarification or add additional context in comments.

Comments

4

Your function needs to return the value

var errorMessage = {
     empty: function(message){
        return "<div class='field'><div class='csv'><span class='icon'></span><label class='manual' id='error-message'>" + message + "</label></div></div>";
    }
 };

console.log(errorMessage.empty("Hello"));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.