0

I was developing a feedback system in my webpage, in which the user inputs name, email adress, and the feedback.

I made a "Discard" button in it. On clicking it, you get a confirmation message saying "Discard this draft?". I wanted the values in input boxes to be cleared if "Ok" is clicked, and the values to remain the same if "Cancel" is clicked.

But whether I click "Ok" or "Cancel", the input boxes always go blank.

Here's the code:

document.getElementById("discard").onclick = function() {
  var choice = confirm("Discard this draft?");

  var Name = document.getElementById("name");
  var Email = document.getElementById("email");
  var Feedback = document.getElementById("feedback");

  var nameValue = Name.value;
  var emailValue = Email.value;
  var feedbackValue = Feedback.value;

  if (choice == null) {
    Name.value = nameValue;
    Email.value = emailValue;
    Feedback.value = feedbackValue;
  } else if (choice != null) {
    Name.value = "";
    Feedback.value = "";
    Email.value = "";
  }
}
<form>
  <input type="email" id="email" placeholder="Enter your email" required><br><br>
  <input type="text" id="name" placeholder="Enter your name (optional)"><br><br>
  <textarea type="text" id="feedback" placeholder="Enter the feedback..." required></textarea><br><br>
  <button id="discard">Discard</button>
  <button id="send">Send feedback</button>
</form>

What is wrong?

2
  • Resetting the values does not make any sense, or am I missing something? Commented May 25, 2021 at 13:09
  • To ferikeem's point - you only need to handle the "OK" for discard - no need to set the values to themselves if the user cancels. Commented May 25, 2021 at 13:13

3 Answers 3

3

confirm() returns true or false so your choice value will be boolean.

That's why choice != null is always true and your input boxes always go blank.

Sign up to request clarification or add additional context in comments.

Comments

0

The result of confirm will never be null

confirm returns a boolean (true or false) indicating whether OK or Cancel was pressed.

Your if-statement has to be updated:

 if (choice === false) {
     Name.value = nameValue;
     Email.value = emailValue;
     Feedback.value = feedbackValue;
 }
 else {
     Name.value = "";
     Feedback.value = "";
     Email.value = "";
 }       

2 Comments

Thanks for the answer. But I think you mean "choice == false" instead of "choice === false".
0

Instead of null use false. So your code should be like this

document.getElementById("discard").onclick = function() {
  var choice = confirm("Discard this draft?");

  var Name = document.getElementById("name");
  var Email = document.getElementById("email");
  var Feedback = document.getElementById("feedback");

  var nameValue = Name.value;
  var emailValue = Email.value;
  var feedbackValue = Feedback.value;

  if (choice == false) {
    Name.value = nameValue;
    Email.value = emailValue;
    Feedback.value = feedbackValue;
  } else {
    Name.value = "";
    Feedback.value = "";
    Email.value = "";
  }
}
<form>
  <input type="email" id="email" placeholder="Enter your email" required><br><br>
  <input type="text" id="name" placeholder="Enter your name (optional)"><br><br>
  <textarea type="text" id="feedback" placeholder="Enter the feedback..." required></textarea><br><br>
  <button id="discard">Discard</button>
  <button id="send">Send feedback</button>
</form>

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.