Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

how can i make the input field to be empty after i click a button?

i tried using userinput.value = '' at the end of function but the input is still not empty. here is the code that i’ve tried

let input = document.querySelector('input')
let list = document.querySelector('ul')
let button = document.querySelector('button')

button.addEventListener('click', addtodo)

function addtodo() {
  let userinput = input.value
  let li = document.createElement('li')
  li.innerText = userinput
  list.appendChild(li)
  // console.log(this)
  console.log(userinput)
}

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

You can reset the value of the input with input.value = ""

let input = document.querySelector('input')
let list = document.querySelector('ul')
let button = document.querySelector('button')

button.addEventListener('click', addtodo)

function addtodo() {
  let userinput = input.value
  let li = document.createElement('li')
  li.innerText = userinput
  list.appendChild(li)
  input.value = ""
}
<input type="text" />
<ul></ul>
<button>button</button>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading