Address Book form with HTML and JS - when clicking i want my addBtn to display the name, number and socialmedia

Viewed 72

What i'm trying to do is to display the name, number and socialmedia of my array at the click of the add Button. Then to remove that same name, number and social media at the same time and then when clicking to also search that name, number and social media. I would be okay just for you to help me with the add button. I don't understand what's wrong with my code. Thank you in advance

    let addBtn = document.querySelector('#addBtn'); 
    let removeBtn = document.querySelector('#removeBtn'); 
    let searchBtn = document.querySelector("#searchBtn");


    let contacts = []; 

    function Contact (name, number, socialmedia) {
        this.name = name;
        this.number = number;
        this.socialmedia = socialmedia;
    }
    
    function createContacts(contactsLength) {
        let arrName = ['', '', '']
        let arrNumber = ['', '', '']
        let arrMedia = ['', '', '']

for(let i=0; i<contactsLength; i++) {
    let name = arrName[Math.floor(Math.random() * arrName.length)]
    let number = arrNumber[Math.floor(Math.random() * arrNumber.length)]
     let socialmedia = arrMedia[Math.floor(Math.random() * arrMedia.length)] 
             
contacts.push(new Contact(name, number, socialmedia))   
        }
}
createContacts(10);
console.log(contacts);


addBtn.addEventListener('click', addContact) 

function addContact() {
    for(const list of contacts) {
        ({name, number, socialmedia} = list)
        console.log(contact.name);
}
}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
<link rel="stylesheet" href="css/style.css">
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
</head>
<body class="bg-dark">

    
<div class="container">
<h1 class="myh1 m-3 text-center">My Address Book</h1> 
<div class="input-group">

    <input id="myInput" type="text" class="form-control mb-3" placeholder="Type Contact" aria-label="Recipient's username with two button addons">
    <button id="addBtn" class="btn btn-outline-info mb-3" type="button">Add</button>
    <button id="removeBtn" class="btn btn-outline-info mb-3" type="button">Remove</button>
    <button id="searchBtn" class="btn btn-outline-info mb-3" type="button">Search</button>
  </div>

<table class="table table-info table-striped" id="myTable">
    <thead>
      <tr>
        <th scope="col" id="name">Name</th>
        <th scope="col" id="number">Number</th>
        <th scope="col" id="socialmedia">Social Media</th>
      </tr>
    </thead>
    <tbody>
  </table>

</div>









  
    <!-- Option 1: Bootstrap Bundle with Popper -->
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/js/bootstrap.bundle.min.js" integrity="sha384-MrcW6ZMFYlzcLA8Nl+NtUVF0sA7MsXsP1UyJoMp4YLEuNSfAP+JcXn/tWtIaxVXM" crossorigin="anonymous"></script>
    <!--  JQuery + mio Javascript -->
    <script src="https://code.jquery.com/jquery-3.5.1.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.19.1/jquery.validate.min.js"></script>
    <script src="js/script4.js"></script>
</body>
</html>

2 Answers

Here is some rudimentary functionality. It is difficult to remove without selecting, so I removed all matches.

const $tableBody = $('#myTable tbody');
const $input = $('#myInput');
const $addBtn = $('#addBtn');
const $removeBtn = $('#removeBtn');
const $searchBtn = $("#searchBtn");

const contacts = [];

function Contact(name, number, socialmedia) {
  this.name = name;
  this.number = number;
  this.socialmedia = socialmedia;
}

function renderContacts(searchName) {
  $tableBody.empty()
    .append(contacts
      .filter(({ name }) => !searchName || (name === searchName))
      .map(({ name, number, socialmedia }) =>
        $('<tr>')
          .append($('<td>').text(name))
          .append($('<td>').text(number))
          .append($('<td>').text(socialmedia))));
}

function addContact(name, number, media) {
  contacts.push(new Contact(name, number, media));
}

function removeContacts(name) {
  for (let i = contacts.length - 1; i >= 0; i--) {
    if (contacts[i].name === name) {
      contacts.splice(i, 1);
    }
  }
}

function createContacts(contactsLength) {
  let arrName = ['Bob', 'Jim', 'Ted']
  let arrNumber = ['21', '42', '84']
  let arrMedia = ['Facebook', 'Snapchat', 'Twitter']

  for (let i = 0; i < contactsLength; i++) {
    let name = arrName[Math.floor(Math.random() * arrName.length)]
    let number = arrNumber[Math.floor(Math.random() * arrNumber.length)]
    let socialmedia = arrMedia[Math.floor(Math.random() * arrMedia.length)]

    addContact(name, number, socialmedia);
  }
}

function onAddContact(e) {
  addContact($input.val(), 0, 'UNKNOWN');
  $input.val('');
  renderContacts();
}

function onRemoveContacts(e) {
  removeContacts($input.val(), 0, 'UNKNOWN');
  $input.val('');
  renderContacts();
}

function onSearchContacts(e) {
  renderContacts($input.val());
}

createContacts(10);
renderContacts();

$addBtn.on('click', onAddContact);
$removeBtn.on('click', onRemoveContacts);
$searchBtn.on('click', onSearchContacts);
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/js/bootstrap.bundle.min.js" integrity="sha384-MrcW6ZMFYlzcLA8Nl+NtUVF0sA7MsXsP1UyJoMp4YLEuNSfAP+JcXn/tWtIaxVXM" crossorigin="anonymous"></script>
<script src="https://code.jquery.com/jquery-3.5.1.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.19.1/jquery.validate.min.js"></script>

<body class="bg-dark">
  <div class="container">
    <h1 class="myh1 m-3 text-center">My Address Book</h1>
    <div class="input-group">

      <input id="myInput" type="text" class="form-control mb-3" placeholder="Type Contact" aria-label="Recipient's username with two button addons">
      <button id="addBtn" class="btn btn-outline-info mb-3" type="button">Add</button>
      <button id="removeBtn" class="btn btn-outline-info mb-3" type="button">Remove</button>
      <button id="searchBtn" class="btn btn-outline-info mb-3" type="button">Search</button>
    </div>

    <table class="table table-info table-striped" id="myTable">
      <thead>
        <tr>
          <th scope="col" id="name">Name</th>
          <th scope="col" id="number">Number</th>
          <th scope="col" id="socialmedia">Social Media</th>
        </tr>
      </thead>
      <tbody>
    </table>

  </div>
</body>

Code snippet for the addition of content.

let addBtn = document.querySelector("#addBtn");
let removeBtn = document.querySelector("#removeBtn");
let searchBtn = document.querySelector("#searchBtn");

let contacts = [];

function Contact(_name, number, socialmedia) {
  this._name = _name;
  this.number = number;
  this.socialmedia = socialmedia;
}

function createContacts(contactsLength) {
  let arrName = ["name1", "name2", "name3"];
  let arrNumber = ["array1", "array2", "array3"];
  let arrMedia = ["arrayMedia1", "arrayMedia2", "arrayMedia3"];

  for (let i = 0; i < contactsLength; i++) {
    let name = arrName[Math.floor(Math.random() * arrName.length)];
    let number = arrNumber[Math.floor(Math.random() * arrNumber.length)];
    let socialmedia = arrMedia[Math.floor(Math.random() * arrMedia.length)];

    contacts.push(new Contact(name, number, socialmedia));
  }
}
createContacts(10);
console.log(contacts);

addBtn.addEventListener("click", addContact);

function addContact() {
  for (const list of contacts) {
    ({ _name, number, socialmedia } = list);
    $("#tableBody").append(
      "<tr><td>" + _name + "</td><td>" + number + "</td><td>" + socialmedia + "</td></tr>"
    );
  }
}
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="css/style.css">
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet"
        integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
</head>

<body class="bg-dark">


    <div class="container">
        <h1 class="myh1 m-3 text-center">My Address Book</h1>
        <div class="input-group">

            <input id="myInput" type="text" class="form-control mb-3" placeholder="Type Contact"
                aria-label="Recipient's username with two button addons">
            <button id="addBtn" class="btn btn-outline-info mb-3" type="button">Add</button>
            <button id="removeBtn" class="btn btn-outline-info mb-3" type="button">Remove</button>
            <button id="searchBtn" class="btn btn-outline-info mb-3" type="button">Search</button>
        </div>

        <table class="table table-info table-striped" id="myTable">
            <thead>
                <tr>
                    <th scope="col" id="name">Name</th>
                    <th scope="col" id="number">Number</th>
                    <th scope="col" id="socialmedia">Social Media</th>
                </tr>
            </thead>
            <tbody id="tableBody">
            </tbody>
        </table>

    </div>

    <!-- Option 1: Bootstrap Bundle with Popper -->
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/js/bootstrap.bundle.min.js"
        integrity="sha384-MrcW6ZMFYlzcLA8Nl+NtUVF0sA7MsXsP1UyJoMp4YLEuNSfAP+JcXn/tWtIaxVXM"
        crossorigin="anonymous"></script>
    <!--  JQuery + mio Javascript -->
    <script src="https://code.jquery.com/jquery-3.5.1.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.19.1/jquery.validate.min.js"></script>
    <script src="js/script4.js"></script>
</body>

</html>

Related