Ok, il y a un problème. Le tableau n'enregistre pas les données de l'entrée. Quand je veux enregistrer un nom, il semble toujours vide et je ne peux rien sortir. Je teste seulement une entrée pour le nom pour être sûr que tout fonctionne, avant que j'écrive un code pour d'autres entrées. Toute aide et suggestions seront appréciées.JS | Le tableau ne sauvegarde pas les données de l'entrée | Solution?
$(document).ready(function() {
$('#newContact').on('click', function() {
$('#dataSection').slideToggle();
$('#buttonSave').delay(400).fadeToggle();
});
});
var contacts = [];
function printPerson(person) {
console.log(person.firstName);
};
var firstName = document.getElementById("firstName").value;
function addFirstName(firstName) {
this.firstName = firstName;
};
function saveContact() {
contacts[contacts.length] = new addFirstName(document.getElementById("firstName").value);
document.getElementById("output").innerHTML = contacts[0];
};
.input-group {
margin: 2px;
padding: 2px;
}
#newContact:hover {
cursor: pointer;
}
#dataSection, #buttonSave {
display: none;
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>AddressBookApp</title>
<link rel="stylesheet" type="text/css" href="addressbook.css">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
</head>
<body>
<div class="container">
<h2>AddressBook App</h2>
<hr class="my-4">
<h5><span class="badge badge-secondary" id="newContact">New contact</span></h5>
<!-- Contact section -->
<div class="alert alert-success" role="alert" id="dataSection">
<!-- Firstname Input -->
<div class="input-group">
<span class="input-group-addon" id="basic-addon1">1</span>
<input type="text" class="form-control" placeholder="Type a first name" aria-label="Name" id="firstName">
</div>
<!-- LastName Input -->
<div class="input-group">
<span class="input-group-addon" id="basic-addon2">2</span>
<input type="text" class="form-control" placeholder="Type a last name" aria-label="Lastname" id="lastName">
</div>
<!-- PhoneNumber Input -->
<div class="input-group">
<span class="input-group-addon" id="basic-addon3">3</span>
<input type="text" class="form-control" placeholder="Type a number phone" aria-label="Number" id="phoneNumber">
</div>
<!-- Email Input -->
<div class="input-group">
<span class="input-group-addon" id="basic-addon4">4</span>
<input type="text" class="form-control" placeholder="Type an email">
</div>
</div>
<!-- Button save provided contact -->
<button onclick="saveContact()" type="button" class="btn btn-info" id="buttonSave">Save</button>
<p id="output"></p>
</div>
<!-- Scripts -->
<!-- Jquery -->
<script src="https://code.jquery.com/jquery-3.2.1.min.js" integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4=" crossorigin="anonymous"></script>
<!-- Bootstrap -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
<!-- JS -->
<script type="text/javascript" src="addressbook.js"></script>
</body>
</html>
'contacts [0]' est un objet, vous devez accéder à ses contacts de propriétés [0] .firstName' – Lixus