I want to set the values to nothing
This will reset the value to nothing for each select
/input
$element.each(function() {
if ($(this).val() != null) {
$(this).val("");
$(this).change();
}
});
...and run their onchange functions which are prepared for a nothing answer
this will be invoked because you have called .change()
on one of those elements
$element.on('change', function() {
$('the_form').reset()
});
It sounds like when that .change()
method is invoked for any element, you want to reset the form...
If these answers are reset and there onchange functions are run, it essentially resets the form...
(Example taken from here)
This is as far as I understand your question, I am unsure when you started talking of other child elements...
...this may hide or show spans based on their answers
But, this code will reset all values of input
/select
and call their .change()
method. The problem with this, what I can see, is that for every element, you are resetting the form which will put additional strain on the DOM and is not ideal.
N.B: If you are not setting the value of these elements, you can use text()
instead which will check the text inside the element
The HTML onchange
attribute only seems to be possible for select
elements. From references I cannot see if this is possible with input
's as well...
Execute a JavaScript when a user changes the selected option of a element:
(Credit w3schools)