diff --git a/frappe/public/js/frappe/form/form.js b/frappe/public/js/frappe/form/form.js index 30ba5eaad9..ec690e7a41 100644 --- a/frappe/public/js/frappe/form/form.js +++ b/frappe/public/js/frappe/form/form.js @@ -647,10 +647,18 @@ frappe.ui.form.Form = class FrappeForm { } focus_on_first_input() { - let first = this.form_wrapper.find(".form-layout :input:visible:first"); - if (!in_list(["Date", "Datetime"], first.attr("data-fieldtype"))) { - first.focus(); + const layout_wrapper = this.layout.wrapper; + + // dont do anything if the current active element is inside the form + // user must have clicked on some element before this function trigerred + if (!layout_wrapper || layout_wrapper.has(":focus").length) { + return; } + + layout_wrapper + .find(":input:visible:first") + .not("[data-fieldtype^='Date']") + .trigger("focus"); } run_after_load_hook() {