My delete eventhandler doesn't work

Hello everyone, I have been trying to make my delete event handler to work but it has been giving me headache. Can some one point out what I’m doing wrong please ? here is the code :

function deleteNote(event, noteId) {
         event.stopPropagation();
        setNotes(prevNotes => {
        const delArray = []
            for(let i = 0; i < prevNotes.length; i++){
                let note = prevNotes[i]
                if(note.id === noteId){
                    delArray.splice(note[i], 1)
            }else {
                delArray.push(note)
            }
            return delArray
        }
        })
        }

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (').

Hi @fixzycode

If deleteNote is an event handler, the first parameter is the event object. What is the second parameter, noteId ?

Thanks man, I’m new on this forum

The second parameter is the id for the event that will be deleted, but any suggestion will be appreciated

As far as I know, the event handler takes 1 parameter the event object. Try console.log(noteId) in the event handler, most likely it is undefined. Read about it here.

It would be nice with some more context. Like how the handler is used and what it is specifically that isn’t working.

Is this just a plain JS project, or is a framework being used?


As an aside, you can pass more arguments to the handler. But it matters how it is done.

<button>Click</button>
const btn = document.querySelector("button");

// Explicit event object passed (parameter order should match the arguments)
btn.addEventListener("click", (e) => handler(e, "arg1", "arg2"));
// Implicit, arg has to be named "event" (parameter order should match the arguments)
// btn.addEventListener("click", () => handler(event, "arg1", "arg2"));

function handler(event, ...args) {
  console.log(event.target); // <button>Click</button>
  args.forEach((arg) => {
    console.log(arg); // arg1, arg2
  });
}