In Page DocType why is change event called twice?

Hello,

I have create a Page DocType.

In this page I have place the code

let inspection_status = page.add_field({
		label: 'Inspection Status',
		fieldtype: 'Select',
		fieldname: 'inspection_status_list',
        //default: 'All',
		options: [
			'All',
			'Pending',
			'Completed'
		],
		change() {
			frappe.msgprint(inspection_status.get_value());
		}
	});

But when a user selects an item the Select’s change event is called twice.

Why is this happening?

Will this affect performance as I am fetching a lot of data using multiple joined SQL statement?

TIA

Yogi Yang

1 Like

I have noticed the same behaviour.

Is defining change() within the inspection_status object and referring to it for it’s own definition correct : inspection_status.get_value()

Is change() the only event we can use?

Also Use Custom Events for Page - #7 by nikzz

1 Like

@EugeneP

Thanks for the link.

I checked the thread but it did not help solve the problem that I am also facing just like @YogiYang.

It’s because the change event is being triggered twice when the option is selected and when the field is blurred.

You can try the below approach.

var is_focused = $(`.page-form [data-fieldname='${field}']`).is(":focus")
if(is_focused)
     $(`.page-form [data-fieldname='${field}']`).blur();
else
    // your code here

3 Likes