最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - reference error in html button on click while referenced in script tag - Stack Overflow

programmeradmin3浏览0评论

I know I have the called function the HTML in a script tag as mentioned above, yet my code is still throwing

ReferenceError: createTask is not defined at HTMLButtonElement.onclick`

when I click the button.

The StackBlitz link (if needed): .js,index.html

function createTask() {
  var x;
  x++;
  var value = 'task'.concat(toString(x));
  class Task {
    constructor(value) {
      super(value, value);
      this.task = {
        value: {
          title: '',
          desc: ''
        }
      };
    }
  }
  const task = new Task(Value);
}
<!DOCTYPE html>
<html lang="en">

<head>
  <title>Home</title>
  <meta charset="UTF-8" />
  <meta name="viewport" content="width=device-width" />
  <link rel="stylesheet" href="styles.css" />
  <script src="script.js"></script>
</head>

<body>
  <textarea id='taskcontent'>
    </textarea>
  <button onclick='createTask()'>
      +
    </button>
  <div id='taskcontainer'>
  </div>
</body>

</html>

I know I have the called function the HTML in a script tag as mentioned above, yet my code is still throwing

ReferenceError: createTask is not defined at HTMLButtonElement.onclick`

when I click the button.

The StackBlitz link (if needed): https://stackblitz/edit/stackblitz-starters-3xxvzunv?file=script.js,index.html

function createTask() {
  var x;
  x++;
  var value = 'task'.concat(toString(x));
  class Task {
    constructor(value) {
      super(value, value);
      this.task = {
        value: {
          title: '',
          desc: ''
        }
      };
    }
  }
  const task = new Task(Value);
}
<!DOCTYPE html>
<html lang="en">

<head>
  <title>Home</title>
  <meta charset="UTF-8" />
  <meta name="viewport" content="width=device-width" />
  <link rel="stylesheet" href="styles.css" />
  <script src="script.js"></script>
</head>

<body>
  <textarea id='taskcontent'>
    </textarea>
  <button onclick='createTask()'>
      +
    </button>
  <div id='taskcontainer'>
  </div>
</body>

</html>

Share Improve this question edited 2 days ago mplungjan 178k28 gold badges181 silver badges240 bronze badges asked 2 days ago sockeatersockeater 32 bronze badges New contributor sockeater is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct. 4
  • 2 What is super()? – mykaf Commented 2 days ago
  • @mykaf developer.mozilla./en-US/docs/Web/JavaScript/Reference/… – sockeater Commented 2 days ago
  • 3 But you are not extending an existing class – mplungjan Commented 2 days ago
  • 2 The code is producing an error before the one you mention in the question, and that error is causing createTask to not be defined. Remove the errant call to super(value, value) and the error you mention doesn't occur. – David Commented 2 days ago
Add a comment  | 

1 Answer 1

Reset to default 0
  1. Remove the super - it gives an error that results in your function not found
  2. Use eventListeners
  3. Delegate the delete click
  4. Be DRY (Don't Repeat Yourself)

class Task {
  static nextId = 1;
  constructor(title) {
    this.id = Task.nextId++;
    this.title = title;
  }
}

window.addEventListener('load', () => {
  const tasks = []; // to hold all tasks
  const taskContent = document.getElementById('taskcontent');
  const addTaskButton = document.getElementById('addTaskButton');
  const taskContainer = document.getElementById('taskcontainer');

  // Render tasks into the container - I use template literals
  const renderTasks = () => {
    taskContainer.innerHTML = tasks
      .map(task => `
        <div class="task-item" data-id="${task.id}">
          <span>${task.title}</span>
          <button class="delete-btn">Delete</button>
        </div>`)
      .join('');
  };

  const createTask = () => {
    const title = taskContent.value.trim();
    if (!title) return;
    tasks.push(new Task(title));
    renderTasks();
    taskContent.focus(); // make it possible to rewrite the task
    taskContent.select();// and overwrite it too. 
  };
  // you COULD use a form and the submit event to not even need to click  - then you would use e.preventDefault() to stop the submission
  addTaskButton.addEventListener('click', createTask); // when clicked
  // Use event delegation to handle delete clicks
  taskContainer.addEventListener('click', e => {
    const tgt = e.target.closest('button.delete-btn'); // is it a delete button
    if (!tgt) return; // no it was not
    const task = tgt.closest('.task-item');
    const taskId = +task.dataset.id;
    const index = tasks.findIndex(task => task.id === taskId);
    if (index > -1) { // did we find the task in the array?
      tasks.splice(index, 1); // delete it
      renderTasks();
    }
  });
});
<textarea id='taskcontent'></textarea>
<button id="addTaskButton" type="button">+</button>
<div id='taskcontainer'>
</div>

发布评论

评论列表(0)

  1. 暂无评论