Ninos
Ninos

Reputation: 43

How can I empty the field of the form after submit?

How can I empty the 'city' field after click button 'Get Weather'? and I have to click twice to the button to get the correct 'time' image

code link : project link

Upvotes: 0

Views: 63

Answers (4)

xadm
xadm

Reputation: 8418

getWeather = async (e) => {
  e.preventDefault();
  // const city = e.target.elements.city.value;
  const element = e.target.elements.city;
  const city = element.value;

  // after data fetching or at once
  element.value="";

Also read docs about uncontrolled components.

Upvotes: 0

Jay
Jay

Reputation: 1309

document.getElementById("city").value = ""

--- This will work if it is an input field, or text area.

Upvotes: 1

Bastiaan Buitelaar
Bastiaan Buitelaar

Reputation: 85

Take a look at this:

<!DOCTYPE html>
<html>
<body>
 
<p>Enter some text in the fields below,
then press the "Reset form" button to 
reset the form.</p>
 
<form id="myForm">
  First name: <input type="text" name="fname"><br>
  Last name: <input type="text" name="lname"><br><br>
  <input type="button" onclick="submit()" value="Reset form">
</form>
 
<script>
function submit() {
  /*Put all the data posting code here*/
 document.getElementById("myForm").reset();
}
</script>
 
</body>
</html>

Upvotes: 0

Flavio Condemi
Flavio Condemi

Reputation: 20

You have to simply write "name_of_object".setText(" ");

Upvotes: 0

Related Questions