0

I'm currently working on a library project, where you have a form where you put different information about a book, and also you have to check a checkbox if you have read the book. Well, the thing that I want to do is, if the checkbox is checked, I want the text content that is gonna be displayed in the html to be Read or if the checbox ain't checked the text content to be Not read. Thanks in advance, and sorry if this question bothers anyone

function intakeFormData() {
  let Title = document.getElementById("Title").value;
  let Author = document.getElementById("Author").value;
  let Pages = document.getElementById("Pages").value;
  let Read = document.getElementById("Read").value;
  if (Read === true) {
    Read.textContent = "Read";

  } else {
    Read.textContent = "Not read";
  }
}
<body>
  <div class="container">
    <div class="form-heading">
      <h1>Add new book to the library</h1>
    </div>
    <div class="books-heading">
      <h1>Current books in the library</h1>
    </div>
    <div class="form">
      <button class="add-book-button">Add new book</button>
      <div id="add-book-form" style="display:none">
        <form id="add-book">
          <label for="Title">Title:</label>
          <input type="text" id="Title" name="Title" />
          <label for="Author">Author:</label>
          <input type="text" id="Author" name="Author" />
          <label for="Pages">Pages:</label>
          <input type="number" id="Pages" name="Pages" />
          <div class="is-read"><label for="Read">Have you read it?</label>
            <input class="checkbox" type="checkbox" id="Read" name="Read" />
          </div>

        </form>
        <button type="submit" class="submit-button">Submit to library</button>
        <button type="reset" class="reset-button">Reset</button>
      </div>
    </div>
    <div class="books"></div>
  </div>
</body>

4
  • 1
    can you post the html also ? Commented Dec 14, 2022 at 12:59
  • you need to look for checked in the ... if (Read.checked) .... Commented Dec 14, 2022 at 13:04
  • The value property is a String. It is never a Boolean. Commented Dec 14, 2022 at 13:08
  • document.getElementById("Read").checked shows a boolean of the checked property Commented Dec 14, 2022 at 16:25

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.