I'm having a Javascript issue that is driving me crazy. I'm trying to hook up a form to a constructor function. It should grab the values of the input fields and then set them equal to the properties in the new Person object.
The problem is that on submit, it's setting the properties equal to empty strings, and I can't figure out why, even though I know the live values are being update on change.
Script
//will hold an array of people objects
var list = [];
//constructor that builds new people to add to address book
function Person(first, last, address, number, email) { //new person constructor
this.firstName = first;
this.lastName = last;
this.address = address;
this.number = number;
this.email = email;
};
// When the submit button is clicked, create a new person and add the values of
// the form fields to the properties of the object
$("#addform").submit(function (e) {
e.preventDefault();
var person = new Person($("input[name = 'fname']").val(),
$("input[name = 'lname']").val(),
$("input[name = 'email']").val(),
$("input[name = 'address']").val(),
$("input[name = 'phone']").val());
list.push(person);
console.log(list);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form id="addform">
<input type="text" name="fname" placeholder="First name">
<input type="text" name="lname" placeholder="Last name">
<input type="email" name="email" placeholder="email">
<input type="input" name="address" placeholder="address">
<input type="tel" name="phone" placeholder="phone number">
<button type="submit" id="submitbtn">Submit</button>
<button type="button" id="closebtn">Close</button>
</form>