Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

282
Views
Adding input value into li element using class

I'm learning JavaScript classes and I want to create an li element with text and a button (the text will be from an input element). The problem is that I can't do it in my method in class because when I const task = this.inputOne.value and after adding it to li.innerHTML, I got undefined. When I console.log every construct value in my method I get undefined, but when I console.log(modulOne.li) outside the class I get the correct li component in console. How do I solve this? Here is my JavaScript code:

class Komponenty {
  constructor() {
    this.form = document.querySelector("form");
    this.inputOne = document.querySelector("input");
    this.buttonAdd = document.querySelector("button");
    this.ulAdd = document.querySelector("ul");
    this.wartosc = this.inputOne.value;
    this.li = document.querySelectorAll("li");
  }
  addTask(e, x) {
    e.preventDefault();
    const taskValue = this.inputOne.value;
    const li = document.createElement("li");
    li.className = "task";
    li.innerHTML = taskValue + "<button>Usuń</button>";
    li.textContent = document.body.appendChild(li);
  }
}

const modulOne = new Komponenty();

And here is my HTML:

<!DOCTYPE html>
<html lang="pl">

<head>
 <meta charset="UTF-8">
 <meta name="viewport" content="width=device-width, initial-scale=1.0">
 <meta http-equiv="X-UA-Compatible" content="ie=edge">
 <title>Projekt - dodawanie zadań - z użyciem tablicy</title>
</head>

<body>
 <form action="">
  <input type="text">
  <button>Dodaj zadanie</button>
 </form>
 <h1>Liczba zadań: <span>0</span></h1>
 <ul></ul>
 <script src="main.js"></script>
</body>

</html>
about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!