0

I want to write a simple function that will display an alert after sending the form, but I'm doing something wrong. Here is my code:

$(function() {

    var alert_container  = $('<div class="alert"></div>');
    var alert = {
        "success": "Success",
        "error":  "Error"
    };

    function show_message(msg) {

        alert_container
            .addClass(msg)
            .append('<p>'+alert.msg+'</p>');

        alert_container.insertBefore($("form")).hide().fadeIn(300);

    }

    $('form').on('submit', function(e) {
        e.preventDefault();
        show_message('success');
    });

});

This code returns to me undefined inside alert box, but if i check console.log(alert.success) returns correct value which is "Success". What am I missing here?

1 Answer 1

5
.append('<p>'+alert.msg+'</p>');

alert.msg does not exist here as it'll try and resolve .msg, not as an array property.

Try: .append('<p>'+alert[msg]+'</p>');

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

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.