0

I’m trying to use innerHTML to add an li item according to user input (in a textbox). Is there any way to create new list item other than using createElement or appendChild?

const user = document.getElementById('btn');
const inputEle = document.getElementById("submit");
const userList = document.getElementById('itemList');

user.addEventListener('click', (e) => {
  userList.innerHTML += `<li>${inputEle.value}</li>`;
});
<input type="text" id="submit">

<button type="button" id="btn">Add</button>

<ul id="itemList"></ul>

8
  • Please have a look stackoverflow.com/questions/17773938/… Commented May 14, 2020 at 10:27
  • 2
    is there a reason for not creating an element or appending a child? Commented May 14, 2020 at 10:29
  • 1
    What is wrong with your proposed solution? I have created a snippet for you and it works fine. Commented May 14, 2020 at 10:32
  • My solution doesn't add multiple li items to my ul , it just adds 1 li item . Commented May 14, 2020 at 10:36
  • 1
    I don't understand. It works for me, I can add as many li as I want with your snippet Commented May 14, 2020 at 10:38

1 Answer 1

0

I found out that the problem is in html file, where i put my button and input inside ul

<ul>
<input type="text"  id="submit" >
<button type="button" id="btn">Add</button>
</ul>
Sign up to request clarification or add additional context in comments.

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.