-2
let formDataContainer = new Array();

function appendFormData(firstNamePar, lastNamePar, messagePar) {

    function Person(firstNamePar, lastNamePar, messagePar){
        this.first_Name = firstNamePar;
        this.last_Name = lastNamePar;
        this.my_Message = messagePar;
    }

    let person1 = new Person(firstNamePar, lastNamePar, messagePar);
    formDataContainer.push(person1);
};

submitButton.addEventListener('click', appendFormData(firstName.value, lastName.value, message.value));
console.log(formDataContainer[0]);

BUT MY OUTPUT IS:

Person {first_Name: '', last_Name: '', my_Message: ''}

AND NOT:

Person {first_Name: 'Raven', last_Name: 'Volt', my_Message: 'Hello world'}

isherwood
  • 58,414
  • 16
  • 114
  • 157
Raven
  • 1
  • 1
  • Please have a look at the tools available in the post editor and use them to properly format your code and messages. Also, your question needs more explanation. See [ask]. – isherwood May 02 '22 at 13:23
  • you are calling console.log when the formDataContainer is still an empty array. The event listener appending data to the array is going to be executed only once the click event will be triggered.. but the console.log was executed as soon as the page was loaded. You should put that console.log inside the event handler function. Plus the way you are adding the listener is weird. You have to pass a function there.. not an invocation like that. Otherwise it will just add an undefined to the click event of that button written like you did. – Diego D May 02 '22 at 13:25
  • 1
    You are calling `appendFormData` and assigning what it returns to the click handler. You are not calling `appendFormData` on click. – epascarello May 02 '22 at 13:25

1 Answers1

0

change the code to this

let formDataContainer = new Array();

function appendFormData(firstNamePar, lastNamePar, messagePar) {

    function Person(firstNamePar, lastNamePar, messagePar){
        this.first_Name = firstNamePar;
        this.last_Name = lastNamePar;
        this.my_Message = messagePar;
    }

    let person1 = new Person(firstNamePar, lastNamePar, messagePar);
    formDataContainer.push(person1);
};

submitButton.addEventListener('click', () => appendFormData(firstName.value, lastName.value, message.value));
console.log(formDataContainer[0]);


you are calling appendFormData when you were declaring the event listener

R4ncid
  • 6,944
  • 1
  • 4
  • 18