0

I'm facing a annoying issue. I can't find a way to clear the form after

the submit button is clicked in this react component . I tried pretty much everything.

is someone able to do this?

I tried window.local.reload, reset, and all other thing.

import emailjs from "emailjs-com";
import "./style.css";
import React from "react";

const About = () => {
  function sendEmail(e) { 
    e.preventDefault()
    document.getElementsByClassName("form-control").value=" " ;

    emailjs
      .sendForm(
        "",
        "",
        e.target,
        ""
      )
      .then((res) => {
        console.log(res);
      })
      .catch((err) => console.log(err));
  }
  return (
    <div className="contact">
      <div
        className="container"
        style={{
          marginTop: "50px",
          width: "50%",
        }}
      >
        <h1 style={{ marginTop: "25px" }}>Contact Form</h1>
        <form className="form-style-6" onSubmit={sendEmail}>
          <label>name</label>
          <input  type="text" name="name" className="form-control" />

          <label>Email</label>
          <input  type="email" name="user_email" className="form-control"  />

          <label>Message</label>
          <textarea  name="message" rows="4" className="form-control" />
          <input
            type="submit"
            
            className="form-control btn btn-primary"
            style={{ marginTop: "30px" }}
            >
              
          </input>
        </form>
      </div>
      
    </div>
  );
};

export default About;

1
  • Are you trying to clear the form, or refresh the page? Commented Jul 21, 2021 at 18:48

1 Answer 1

1

just add an id for the textbox like:

  <input
    type="text"
    name="from_name"
    id="fieldName"
    className="form-control"
  />

and put this into the code:

...
     .then(
        (result) => {
          console.log(result.text);
        },
        (error) => {
          console.log(error.text);
        }
      );
    document.getElementById('fieldName').value = '';
  };
...
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.