hello I made a to do list app and I want to clear input after submit here is my code but it doesn't workenter image description here
I expect input section to be null after I submit but every time I have to use backspace then write a new task
CodePudding user response:
First, I would change to id="myinput".
You are assigning user_input to the value of the input at that moment.
Replace your code line to get value by id:
let user_input=document.getElementById("myinput");
let user_input_value=user_input.value;
compare: if(user_input_value!='')
clear with: user_input.value='';
CodePudding user response:
Hi
You just need to save the input element in a variable and set the value property to empty string rather than directly setting user_input = ''
.
Also, unless there many inputs you need to loop through, it's better to use id and document.getElementById
to identify the input you want rather than document.querySelectorAll
- Save the input element as
const
const user_input = document.getElementById('myInputId');
// get the value and use as needed
let user_input_value = user_input.value;
- After, when you need to reset, set the input elements value to
''
user_input.value = '';