I am looking for a way to clear all elements found within an HTML form contained inside a Bootstrap modal without refreshing the page.
Currently:
The user enters data and closes the modal.
When the user re-opens the modal, the previously entered data still remains.
How can I completely clear all elements within the form during the close event of the modal dialog so that when the user re-opens it, they always get fresh clean inputs & etc?
I went with a slightly modified version of @shibbir's answer:
Called this way:
This is a variance of need to reset body to original content. It doesn't deal with a form but I feel it might be of some use. If the original content was a ton of html, it is very difficult to string out the html and store it to a variable. Javascript does not take kindly to the line breaks that VS 2015/whatever allows. So I store original ton of html in default modal like this on page load:
Which allows me to to reuse this content when original default button for modal is pressed (there are other buttons that show other content in same modal).
If I put an alert for the styleString variable it would have an endless string of all the html with no breaks but does it for me and keeps it out of VS editor.
Here is how it looks in Visual Studio inside default modal. Saved in the string variable whether automatic (.html) or by hand in VS, it would be one big line:
Just set the empty values to the input fields when modal is hiding.
You can make a JavaScript function to do that:
and then you can call that function each time your modal is hidden:
If you using ajax to load modal's body such way and want to be able to reload it's content
use
The below solution solved my problem and also kept the default values