0

I am making a TODO list. I have difficulties with setting the input text on my card. Everything I write in the input, I want to select and to put on the card.

I tried to select the innerHTML of an input when I type something in. I don't know how to select the typed input text. I would then create a new element with the text inside, and would append it to the card.

let btn = document.querySelector('.add');
let textspace = document.querySelector('.todotext');

const input = document.querySelector('input');

// eventlistner by button clicked

btn.addEventListener('click', function() {
  var txt = document.getElementsByClassName('input').innerHTML;
});
<div class="card">
  <div class="todoheader">TODO List</div>
  <div class="todotext"></div>
  <ul class="list"></ul>
  <div class="addtodo">
    <buton class="add" type="button"> + </buton>
    <input type="text" class="input" placeholder="add todo" />
  </div>
</div>

1
  • Assign the unique id to your input field and use document.getElementById('uniqueID').value instead document.getElementsByClassName() Commented Nov 8, 2022 at 10:51

5 Answers 5

1

To get the value of the input use the value property, not innerHTML. Also note that you already have a reference to the input Element in the input variable, so you don't need to use getElementsByClassName() to retrieve it - not least of all because the syntax there is flawed.

Once you have the text you can use createElement() to add a new p element to the .todotext container:

const btn = document.querySelector('.add');
const textspace = document.querySelector('.todotext');
const input = document.querySelector('input');

btn.addEventListener('click', function() {
  const txt = input.value;  
  if (!txt)
    return;
  
  const p = document.createElement('p');
  p.textContent = txt;
  textspace.appendChild(p);
  input.value = '';
});
<div class="card">
  <div class="todoheader">TODO List</div>
  <div class="todotext"></div>
  <ul class="list"></ul>
  <div class="addtodo">
    <button class="add" type="button"> + </button>
    <input type="text" class="input" placeholder="add todo" />
  </div>
</div>

Sign up to request clarification or add additional context in comments.

Comments

1

As others already answered your question. I just wanted to point out that there is a misspelling in your code (buton instead of button). Fix that and the button element would be rendered correctly.

Comments

0

First, getElementsByClassName will return HTMLCollection which is array-like, you need to use querySelector instead, also the innerHTML, textContent, innerText properties all are empty string for the input because the input is a self-closing tag without content inside, you need to use value property.

let btn = document.querySelector('.add');
let textspace = document.querySelector('.todotext');
const input = document.querySelector('input');

// eventlistner by button clicked

btn.addEventListener('click', function() {
  var txt = input.value;
  console.log(txt)
});
<div class="card">
  <div class="todoheader">TODO List</div>
  <div class="todotext"></div>
  <ul class="list"></ul>
  <div class="addtodo">
    <buton class="add" type="button"> + </buton>
    <input type="text" class="input" placeholder="add todo" />
  </div>
</div>

Comments

0
btn.addEventListener('click', function() {
  var txt = document.getElementByClassName('input').value;
});

You were using getElementByClassName which will be HTMLCollection.To loop over each element you need to convert it into an array loop over them.

Comments

0

var txt = document.getElementsByClassName('input').value;

You should get the value of an input , not the innerHTML

Also assign a unique id to you input fields and select them with it, it's much better :)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.