Clearing all elements in the form in javascript

Hello;
How I can clear (reset) all the elements in the document in javascript?
I tried the following and none of them worked:
cur_frm.doc.reset();
document.getElementById(cur_frm).reset();
document.forms[cur_frm].reset();

I am sure there should be a way.
Thanks for all.
Regards
Bilal

There is no such method as refresh. What is your use-case?

Hello;

In the below document which is used to do clearance (payment settlement) with the drivers, when selecting the driver, the data in the upper side (red rectangle) will be fetched, then the user has to select clearance date and click on Get Data to fill the data in the lower part (below green rectangle).

Until now, every thing is fine.

If need to select another driver, so the data in the upper side will be fetched. But in this case, the below data (in the green rectangle) is related to the previous driver and that is wrong, it should be cleared (becoming blank), and when the user select the new data and click on Get Data, then the corresponding data for the new driver should be fetched in the below part.

Regards
Bilal

You can clear individual fields by using frm.set_value('fieldname', '')

This for certain field, I need to clear all fields in the form.
Actually what I need to do when selecting the Employee is to store the selected value first and then clearing all fields and then re-assign employee value (from the stored value) and then I will fetch the needed values based on the Employee value.

Regards
Bilal

In HTML for example, there is Reset button that clear all the form.
I as asking about same thing equivalent to be used in .js in erpnext.

Regards
Bilal

The only alternative I can think of to @rmehta’s answer is to call .each() on fields_dict which is dangerously lazy. I think you want the control afforded by setting each of the fields to ""with a cur_frm.set_value() call.

As for what you’re actually trying to do, this sounds like a classic callback structure. Store a value (into the database, which implicitly means a callback structure, though the function may have been written as a helper by the Frappe team so part of it is obscured from you), do a lookup based on that value (again a database call) and then populate a from with the returned values.

I don’t know what you mean by “in HTML”, it’s too general a context.

I think he means in HTML, you can add a button of type “reset” which will automatically clear all the other fields in the HTML <form> .

Eg. in the code below, if you click the reset button, the fname and lname fields will get cleared.

<form action="/action_page.php" method="get">
  First name: <input type="text" name="fname"><br>
  Last name: <input type="text" name="lname"><br>
  <button type="submit" value="Submit">Submit</button>
  <button type="reset" value="Reset">Reset</button>
</form>
1 Like

@littlehera
Exactly, this is what I meant.
So how can we do same thing in ERPNext but instead of having Reset button, how it is possible to be done through script depending on the need?
Regards
Bilal

You can add a reset button using frm.add_custom_button api