0

I have a field that inserts HTML when a user submits my form. If there is an error it inserts the error message before the form field. However, if the user submits multiple times it inserts multiple error messages, I would like to simply replace the error message so that it only appears once. I've tried to use replace but I couldn't get this way to work.

The form works via ajax so we don't reload the page to remove the errors, I need a way of replacing / removing them using jQuery.

Here is my jQuery code.

    $('<p class="form-error" id="' + errorKey + 'ErrorMessage">' + json.errors[errorKey] + '</p>')
.insertBefore(('[name="' + errorKey + '"]'))
5
  • 1
    Have you tried to disable submit button during sending data from the form? Commented Oct 19, 2017 at 7:39
  • The user can submit again if there are errors Commented Oct 19, 2017 at 7:44
  • The form works via ajax, so the page won't reload and remove the errors Commented Oct 19, 2017 at 7:44
  • 1
    Instead of inserting the error, could you not simply have an element whos visibility is toggled on/off on fail/success and populated with the error text? Commented Oct 19, 2017 at 7:47
  • Simple solution: Add an empty div with a unique id and add your created error message with .html Commented Oct 19, 2017 at 7:52

1 Answer 1

2

Select by id and remove:

$('#' + errorKey + 'ErrorMessage').remove();
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.