Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <!--   <link rel="stylesheet" href="style.css"> -->
  <script src="https://kit.fontawesome.com/97a78fa498.js" crossorigin="anonymous"></script>

  <title>Address Book</title>
</head>

<body>
  <div class="container" id="fixedContact">
    <form action="#" class="contactForm" name="contactForm">
      <div class="firstContactForm">
        <h2>New Contact</h2>
        <div class="contactControl">
          <label for="firstName">First name</label>
          <input type="text" placeholder="First name" id="firstName" class="clearForm" required>
        </div>
        <div class="contactControl">
          <label for="lastName">Last name</label>
          <input type="text" placeholder="Last name" id="lastName" class="clearForm" required>
        </div>
        <div class="contactControl">
          <label for="phone">Phone</label>
          <input type="text" placeholder="Phone" id="phone" class="clearForm" required>
        </div>
        <div class="contactControl">
          <label for="address">Address</label>
          <textarea name="address" id="address" placeholder="Address" class="clearForm" required></textarea>
        </div>
        <button class="createBtn" id="btnSave">Save</button>
      </div>
    </form>
  </div>

  <div class="contactList container">
    <form action="#">
      <input type="text" placeholder="Search for contact..." class="search">
      <div class="savedContactInfoList">
        <!-- <div class="savedContactInfo" id="savedContactInfo">
                    <div class="savedContact">
                        <i class="fas fa-user"></i>
                        <p type="text" name="savedContactFirstName" class="savedContactFirstName" value=""></p>
                    </div>
                    <div class="savedContact">
                        <i class="fas fa-user"></i>
                        <p type="text" name="savedContactLastName" class="savedContactLastName" value=""></p>
                    </div>
                    <div class="savedContact">
                        <i class="fas fa-phone-alt"></i>
                        <p type="text" name="savedContactPhone" class="savedContactPhone" value=""></p>
                    </div>
                    <div class="savedContact">
                        <i class="fas fa-map-marker-alt"></i>
                        <p type="text" name="savedContactAddress" class="savedContactAddress" value=""></p>
                    </div>
                    <div>

                        <button type="button" class="btnDel" id="btnDel">Delete</button>
                    </div>
                </div> -->
      </div>
    </form>

  </div>
  <script src="https://code.jquery.com/jquery-3.5.1.min.js" integrity="sha256-9/aliU8dGd2tb6OSsuzixeV4y/faTqgFtohetphbbj0=" crossorigin="anonymous"></script>
  <script src="script.js"></script>
</body>

</html>
              
            
!

CSS

              
                @import url("https://fonts.googleapis.com/css2?family=Open+Sans:wght@300;400;600&display=swap");
*,
*::after,
*::before {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

*:focus {
  outline: none;
}

html {
  font-size: 62.5%;
}

body {
  background-color: #f9fafb;
  font-family: "Open Sans", sans-serif;
  /* display: flex; */
  padding: 0 20em;
}

#fixedContact {
  position: fixed;
}

.container {
  width: 30%;
  margin: 4rem auto auto 1em;
  padding: 2em;
  font-size: 2rem;
  background-color: #fff;
  border-radius: 5px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.3);
}

h2 {
  margin: 0 0 2rem;
  text-align: center;
}

.contact-form {
  padding: 3rem 4rem;
  width: 100%;
}

textarea {
  resize: none;
  max-height: 70px;
}

.contactControl {
  padding-bottom: 1em;
}

.contactControl label {
  color: #777;
  display: block;
  margin-bottom: 0.5em;
}

.contactControl input,
textarea {
  border: 2px solid #f0f0f0;
  border-radius: 4px;
  display: block;
  width: 100%;
  padding: 0.7em;
  font-size: 14px;
}

#btnSave {
  width: 100%;
  padding: 0.5em;
  color: white;
  border-radius: 4px;
  border: none;
  cursor: pointer;
  font-size: 2rem;
  background-color: #3498db;
}

.contactList {
  width: 30%;
  right: 0;
  left: 35em;
  position: absolute;
}

.contacts-container {
  display: flex;
}

.savedContactInfo {
  background-color: #fff;
  border-radius: 5px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.3);
  width: 100%;
  padding: 1rem;
  margin-bottom: 1rem;
  /* display: none; */
}

.search {
  width: 100%;
  padding: 1rem;
  margin-bottom: 1.5rem;
  border: none;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.3);
}

.savedContact {
  display: flex;
  margin-bottom: 0.5em;
}

.savedContactFirstName,
.savedContactLastName,
.savedContactPhone,
.savedContactAddress {
  width: 100%;
  border: none;
  color: #333;
  margin-left: 1em;
  font-size: 15px;
}

.btnDel {
  padding: 0.5em 1em;
  margin-top: 1em;
  cursor: pointer;
  background-color: #e74c3c;
  color: white;
  border: none;
  border-radius: 5px;
}

              
            
!

JS

              
                let contactsStorage = [];
// let contactCardArr = [];
let createBtn = document.querySelector(".createBtn");
let deleteBtn = document.querySelector(".btnDel");
let contactCard = document.querySelector(".savedContactInfo");
let contactCardContainer = document.querySelector(".savedContactInfoList");
let firstName = document.querySelector("#firstName");
let lastName = document.querySelector("#lastName");
let phone = document.querySelector("#phone");
let address = document.querySelector("#address");

let searchContactList = document.querySelector(".search");

createBtn.addEventListener("click", createContactCard);
contactCardContainer.addEventListener("click", deleteContactCard);
searchContactList.addEventListener("input", searchContact);

// disable the form submit just in case the user press enter in the search box
const form = document.querySelector(".contactList form");
form.addEventListener("submit", (e) => e.preventDefault());

// Creates a new contact card
function createContactCard(e) {
  e.preventDefault();
  let isNull =
    firstName.value != "" &&
    lastName.value != "" &&
    phone.value != "" &&
    address.value != "";
  if (isNull) {
    // push contact object to contactsStorage
    contactsStorage.push({
      firstName: firstName.value,
      lastName: lastName.value,
      phone: phone.value,
      address: address.value
    });
  }
  // call display from inside create, instead of create from inside display
  displayContactCard();
  clearContactForm();
}

// let htmlStr = "";

// filteredContacts parameter is for an optional argument
function displayContactCard(filteredContacts = null) {
  let htmlStr = "";
  contactCardContainer.innerHTML = "";

  // use filteredContacts if truthy (i.e. displayContactCard was called with an argument)
  // otherwise use contactsStorage
  const contacts = filteredContacts || contactsStorage;

  console.log({ contacts });

  // forEach instead of for loop
  contacts.forEach((contact, i) => {
    htmlStr += `<div class="savedContactInfo" id="savedContactInfo">
                    <div class="savedContact">
                    <i class="fas fa-user"></i>
                      <p type="text" name="savedContactFirstName" class="savedContactFirstName" value="">
                        ${contact.firstName}
                      </p>
                  </div>
                  <div class="savedContact">
                    <i class="fas fa-user"></i>
                      <p type="text" name="savedContactLastName" class="savedContactLastName" value="">
                        ${contact.lastName}
                      </p>
                  </div>
                  <div class="savedContact">
                    <i class="fas fa-phone-alt"></i>
                      <p type="text" name="savedContactPhone" class="savedContactPhone" value="">
                      ${contact.phone}
                      </p>
                  </div>
                  <div class="savedContact">
                    <i class="fas fa-map-marker-alt"></i>
                      <p type="text" name="savedContactAddress" class="savedContactAddress" value="">
                      ${contact.address}
                      </p>
                  </div>
                  <div>
                      
                      <button type="button" class="btnDel" data-id="${i}">Delete</button>
                  </div>
              </div>`;
  });

  contactCardContainer.innerHTML = htmlStr;
  console.log(document.querySelectorAll(".savedContactInfo"));
}

// Search for contact
function searchContact() {
  let searchValue = searchContactList.value.toLowerCase();

  // filter contactsStorage and return a new array with only the search matches
  const filteredContacts = contactsStorage.filter((contact) => {
    return contact.firstName.toLowerCase().includes(searchValue);
  });

  console.log({ filteredContacts });

  // call displayContactCard pass in the filtered list
  displayContactCard(filteredContacts);

  searchValue = "";
}

// Remove a contact card from the addressbook

function deleteContactCard(e) {
  if (e.target.classList.contains("btnDel")) {
    let pos = e.target.getAttribute("data-id");
    contactsStorage.splice(pos, 1);
    displayContactCard();
  }
}

// Clear contact page

function clearContactForm() {
  let clearForm = document.querySelectorAll(".clearForm");
  for (let i in clearForm) {
    clearForm[i].value = "";
  }
}

              
            
!
999px

Console