How can I get an access to removeBtn without unnecessarily invoking renderBook() function this code? (vanilla JS)

I’m a beginner in programming and I’m making a simple interface where a user can remove the card on a webpage by clicking the remove button.

And now I’m trying to implement this removing functionality.

The whole code is in’test’ branch here, not ‘main’. https://codesandbox.io/p/github/Eveieve/library/test?workspaceId=5fe3d173-5c43-47d3-9b21-7447f2999e60&file=%2Fscript.js&selection=%5B%7B%22endColumn%22%3A2%2C%22endLineNumber%22%3A73%2C%22startColumn%22%3A1%2C%22startLineNumber%22%3A59%7D%5D

Currently I have a rendering function where I render elements to the page, including the remove button. Since I want to be able to click that and remove the book, I need an access to the remove button.

Problem is I don’t know how I can get an access to the remove button outside of the rendering function without unnecessarily invoking it (this throws an error, and i’m aware why it’s causing it)

The error is

Uncaught TypeError: Cannot read properties of undefined (reading 'title')
    at renderBook (script.js:44:33)
    at script.js:68:23```
//some code....
  // create removeBtn
  const removeBtn = document.createElement("button");
  removeBtn.classList.add = ("remove-btn", "property");
  removeBtn.textContent = "Remove";
  card.appendChild(removeBtn);

  return { removeBtn };
}

const { removeBtn } = renderBook();

function removeBook(idOfBook) {
  const filteredLibrary = myLibrary.filter((book) => book.id !== idOfBook);
  return { filteredLibrary };
}

I tried to ‘unpack’ removeBtn variable from renerBook() function but since I’m invoking it, the function gets called even before user adds a book so it throws me an error that whatever variable used in the renderBook() is not defined.

How can I get access to the removeBtn without unnecessarily invoking the function?

Or, is it a bad idea to have removeBook function outside the Class? Is it better to have them inside the Class as it’s a functionality that every instance of Book needs?

I separated the functions to make my code more organized but I always encounter this issue of not having access to variables that are scoped to the function. I was wondering if there’s a better way to go about this, or if there’s a fix that can be done to my code.

I’m aware I can just dump everything in the global scope, but I wanted to practice organizing the code better. Let me know if anything’s unclear,appreciate the help.