I have a single form which sends emails to people. There are two buttons to do this: New and Reply.
readonly
to the subject field.However if someone clicks on Reply and then changes their mind to actually send a New email, I have lost all the default values and attributes for the Form. I can't enter these values in manually because the values come from a database dynamically.
I can't get my head around how to switch between New and Reply on the page. I guess I could reload the page when someone clicks New but is that the only way?
EDIT:
I should have mentioned I need to reset the values of Hidden fields as well. When I say "reset" I actually mean go back to their original values.... not clear the value
attribute.
You can easily reset all form values using the HTML button using <input type=”reset”> attribute.
The <input type="reset"> defines a reset button which resets all form values to its initial values.
Using reset buttons<input type="reset"> buttons are used to reset forms.
If you use uncontrolled form inputs, an input type of reset will reset the form inputs to their original values. Using controlled inputs, you must set each input state variable value to its default values.
That's exactly what the reset method does.
$("#myform")[0].reset();
https://developer.mozilla.org/en-US/docs/Web/API/HTMLFormElement.reset
You can also get the default value of an input by getting its value attribute or its default value property (assuming you haven't manually changed them, but you should never do that.)
var val = $("#theinput").attr("value");
and
var val = $("#theinput").prop("defaultValue");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With