-3

when i submit any value, i want to reset textbox automatically. any suggestions how can i do that? my code is as follows:

<!DOCTYPE html>
<html>
<body>
<ul id="myList">
</ul>
<input type="text" id="myText" value="">
<button onclick="myFunction()">SUbmit</button>
<script>
function myFunction() {
  var element = document.createElement("li");
  var textnode = document.createTextNode(myText.value);
  element.appendChild(textnode);
  var list = document.getElementById("myList");
  list.insertBefore(element, list.childNodes[0]);
}
</script>
</body>
</html>
Alex Wayne
  • 178,991
  • 47
  • 309
  • 337

1 Answers1

1

In your submit handler you just first to find that field. You can do that with getElementById():

var myText = document.getElementById('myText')

And then, set it's value to an empty string with:

myText.value = '';

<!DOCTYPE html>
<html>
<body>
<ul id="myList">
</ul>
<input type="text" id="myText" value="">
<button onclick="myFunction()">SUbmit</button>
<script>
function myFunction() {
  var myText = document.getElementById('myText');
  var element = document.createElement("li");
  var textnode = document.createTextNode(myText.value);
  element.appendChild(textnode);
  var list = document.getElementById("myList");
  list.insertBefore(element, list.childNodes[0]);
  myText.value = '';
}
</script>
</body>
</html>
Alex Wayne
  • 178,991
  • 47
  • 309
  • 337