简体   繁体   中英

Why do I get 'cannot read property of value at null' error?

I am trying to create a basic to do list app and currently trying to append the taskInput.value text node to the li. However, when testing it out in the console, I get the error 'Uncaught TypeError: Cannot read property 'value' of null'. Why is this?

<html lang="en">
  <head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

    <!-- Bootstrap CSS -->
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">

    <title>Hello, world!</title>
  </head>
  <body>
      <div class="container">
    <form id="form-input">
      <div class="form-group">
        <label class="h2 mb-2" for="Enter Task">Enter Task</label>
        <input type="text" class="form-control" id="#taskInput" name="taskInput" placeholder="Enter your task...">
      </div>
      <button type="submit" class="btn btn-primary btn-block">Submit</button>
    </form>
    <div class="task-list">
    <h3 class="mt-3">Task List</h3>
    <ul class="list-group">
    </ul>
    </div>
    </div>

    <!-- Optional JavaScript -->
    <!-- jQuery first, then Popper.js, then Bootstrap JS -->
    <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
    <script src="https://kit.fontawesome.com/732aafddc7.js"></script>
    <script src="app.js"></script>
  </body>
</html>
const form = document.querySelector('#form-input');
const taskInput = document.querySelector('#taskInput');
const taskList = document.querySelector('ul.list-group');

form.addEventListener('submit', addTask);


function addTask(e) {
    // create element
    const li = document.createElement('li');
    // add class
    li.className = 'list-group-item d-flex justify-content-between';
    // create text node and append
    li.appendChild(document.createTextNode(taskInput.value));

    console.log(li);
    e.preventDefault();
}

li.appendChild(document.createTextNode(taskInput.value)); is incorrect. You should create an element first and then get its value:

li.appendChild(document.createTextNode(taskInput));

const value = document.getElementById('taskInput').value;

Edit: also looking at your html you'd change id="#taskInput" to id="taskInput" and then in your JS you can get it as: const taskInput = document.getElementById('taskInput');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM