Why it keep editing only the first note even if I click the second one?

  css, debugging, html, javascript, windows

I’m trying to do a Note Taker application using HTML, CSS, Bootstrap, Javascript, with an "Edit" button for every note that I take.Everything works just fine, except that when I add 2, 3, or more notes, and I click the edit button for the 3rd one, I can edit the first one(WTF?! :)) ), it’s the same when I click the 2nd one, and so on.Everytime it keeps editing only the first note.Could someone help me solve this problem, please?

Here’s my HTML:

<!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">
    <link rel = 'stylesheet' href = 'app.css'>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
    <title>Document</title>
</head>
<body>
<div class="head-container">
    <h1 class = 'display-3 text-center text-muted my-4'>Note Taker</h1>
</div>
<div class="row d-flex justify-content-center">
<div class="form-group w-50">
    <input type = 'text' placeholder="Title of your note" id = 'title'>
    <textarea class="form-control width:50% text-center" id="exampleFormControlTextarea1" rows="3" placeholder="Enter your text"></textarea>
    <div class="row d-flex justify-content-center">
    <button id = 'add'>Add Note</button>
</div>
</div>
</div>
<div class="card">
    <h5 class="card-header text-center">My Lists</h5>
    <div class="card-body">
      <dbody id = "notesCard">
        
      </dbody>
    </div>
  </div>

    
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
<script type = 'text/javascript' src = 'app.js'></script> 
</body>
</html>

Here’s my CSS:

#add{
    margin-top: 10px;
    background: none;
    border: none;
    cursor: pointer;
}

#add:hover{
    color:orange;
}

input{
    border: none;
    border-bottom: 1px solid green;
    float: left;
}

.deleteButton{
    color:rgb(3, 139, 3);
    cursor: pointer;
}

.editButton{
    cursor: pointer;
    
}

.saveB{
    cursor: pointer;
}

And here’s my Javascript:

const btn = document.getElementById('add');

const notes = JSON.parse(localStorage.getItem("notes")) || [];

function addNote() {
    const title = document.getElementById('title').value;
    const txt = document.getElementById('exampleFormControlTextarea1').value;

    const note = {
        title,
        txt,
        id: notes.length > 0 ? notes[notes.length - 1].id + 1 : 1,
    };

    notes.push(note);
    localStorage.setItem("notes",JSON.stringify(notes));
    showNote();
}


const showNote = () => {
    const cardBody = document.getElementById('notesCard');
    
        cardBody.innerHTML = "";

        for(let i = 0; i < notes.length; i++){
            cardBody.innerHTML += `
            <div class = "card" style = "1px solid; margin-bottom:10px">
            <h5 class = "card-title" style = "color:#008000">${notes[i].title}</h5>
            <p class = "card-text">${notes[i].txt}</p>
            <div class = "buttons">
            <a class = "deleteButton" style = "color:#FF0000;border:1px solid red; width:50px" onclick = deleteButton(${notes[i].id})>Delete</a>
            <a class = "editButton" style = "color:#FF0000; border:1px solid red; width:50px" onclick = editButton(${notes[i].id})>Edit</a>
            <a class = "saveB" style = "color:#FF0000; border:1px solid red; width:50px; display:none" onclick = saveButton(${notes[i].id})>Save</a>
            </div>
            </div>
            `;

            
        }


}
showNote();

const editButton = (id) => {
    for(let i = 0 ; i < notes.length; i++){
        if(notes[i].id === id){
            const para = document.querySelector(".card-text");
            const editb = document.querySelector('.editButton');
            const saveB = document.querySelector('.saveB');
            para.contentEditable = "true";
            editb.style.display = "none";
            saveB.style.display = "block";
        }
    }
}

const saveButton = (id) => {
    for(let i = 0 ; i < notes.length; i++){
        if(notes[i].id === id){
            const para = document.querySelector(".card-text");
            const editb = document.querySelector('.editButton');
            const saveB = document.querySelector('.saveB');
            para.contentEditable = "false";
            editb.style.display = "block";
            saveB.style.display = "none";
        }
    }
}
const deleteButton = (id) => {
    for(let i = 0; i < notes.length; i++){
        if(notes[i].id === id){
            notes.splice(i,1);
        }
    }

    showNote();
    localStorage.setItem('notes',JSON.stringify(notes));
}

btn.addEventListener('click', () => {
    addNote();
})

Source: Windows Questions

LEAVE A COMMENT