I am working on a small project where by i need to create objects with form field data. In short i have a constructor function that retrieves values from each form field like this:
var Task = function() {
this.title = document.getElementById("task-title").value;
this.date = document.getElementById("task-date").value;
this.option = document.getElementById("task-option").value;
}
What i need is create a NEW instance of object each time somebody clicks the submit button. What i have so far is this:
var submitBtn = document.getElementById('#someID');
submitBtn.addEventListener('click', function(event) {
event.preventDefault();
var newTask = new Task();
});
This works in that it create an object, but the objects gets overridden each time the button is pressed. I need a new instance of the object to be created each time the buttoned is pressed.
Is this possible ? if not is there a more viable alternative to constructor functions? Thank you so much.
new Task();will always created a new object. What do you mean by "the objects gets overridden"? What exactly are you doing withnewTask?var Task = function() {}andfunction Task() {}are the same thing. The OP is already doingnew Task()too.