PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Saturday, October 15, 2022

[FIXED] How to grab the value from an input and store the input value as the text of a new list item?

 October 15, 2022     dom, html, javascript     No comments   

Issue

When the user clicks the button with the ID of generate-todo, I want to grab the value from the input with the ID of new-todo, and store the input value as the text of a new list item and append that new item to the unordered list with the class of todos. This is what I've tried so far but I'm missing something.

  let todo = document.querySelector('#generate-todo').addEventListener('click', function() { 
        todo = document.querySelector('#new-todo').value;
      const create = document.createElement('li');
      document.querySelector('.todos').append(create);  
  });
<label for="new-todo">Add a new item to the list:</label>
<input id="new-todo" type="text">
<button id="generate-todo">Add to the List!</button>
<h1>List of things to do</h1>
<ul class="todos">
</ul>


Solution

this should get you started

document.getElementById('generate-todo').addEventListener('click', function() { 
        let todo = document.getElementById('new-todo').value;
      const li = document.createElement('li');
      li.innerText = todo;
      document.getElementById('todos').append(li);  
  });
<label for="new-todo">Add a new item to the list:</label>
<input id="new-todo" type="text">
<button id="generate-todo">Add to the List!</button>
<h1>List of things to do</h1>
<ul id="todos">
</ul>



Answered By - DCR
Answer Checked By - Dawn Plyler (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing