Blur fires after setting focus on newly added input element

Five years ago a question like what I will be asking today was asked but apparently no answers were posted that would solve the problem. Now I am having the same problem. Here is what I’ve done, so far.

    #cellHandlers = {
        lvCell: null,
        outerDiv: null,
        td: null,
        span: null,
        input: null,
        commitChanges: undefined,
        beginEdit: (td, cell) => {
            this.#cellHandlers.lvCell = cell;
            this.#cellHandlers.td = td;
            this.#cellHandlers.outerDiv = td.firstChild;
            this.#cellHandlers.span = td.querySelector('span');
            if (this.#cellHandlers.input == null) {
                this.#cellHandlers.input = document.createElement('input');
                this.#cellHandlers.input.type = 'text';
                //this.#cellHandlers.input.setAttribute('autofocus', 'true');
            }

            this.#cellHandlers.outerDiv.remove();
            this.#cellHandlers.td.appendChild(this.#cellHandlers.input);
            this.#cellHandlers.td.focus();
            this.#cellHandlers.input.select();
            // using setTimeout is a work around. The original code was
            //this.#cellHandlers.hookEvents(true);
            //this.#cellHandlers.input.focus();
            window.setTimeout(() => {
                this.#cellHandlers.hookEvents(true);
                this.#cellHandlers.input.focus();
            }, 10);
            //this.#cellHandlers.input.focus();
        },
        filterKey: e => {
            e.stopPropagation();
            const result = typeof this.#cellKeyFilter == 'function' &&
                this.#cellKeyFilter({
                    cell: this.#cellHandlers.cell,
                    key: e.key || e.data,
                    value: this.#cellHandlers.input.value,
                    pos: this.#cellHandlers.input.selectionStart
                });
            if (result)
                return true;
            e.preventDefault();
            return false;
        },
        hookEvents: a => {
            for (let h in this.#cellHandlers) {
                if (h[0] != '_')
                    continue;
                if (a)
                    this.#cellHandlers.input.addEventListener(h.substr(1), this.#cellHandlers[h])
                else
                    this.#cellHandlers.input.removeEventListener(h.substr(1), this.#cellHandlers[h])
            }
        },
        endEdit: e => {
            if (e !== undefined) {
                e.stopPropagation();
            }

            if (this.#cellHandlers.outerDiv) {
                this.#cellHandlers.hookEvents(false);
            }

            if (this.#cellHandlers.commitChanges) {
                this.#cellHandlers.commitChanges = false;
                if (this.#cellHandlers.span.textContent != this.#cellHandlers.input.value) {
                    this.#cellHandlers.span.textContent = this.#cellHandlers.input.value;
                    this.dispatchEvent(ListView.#evCellValueChanged);
                }
            }

            try {
                this.#cellHandlers.input.remove();
            }
            catch {

            }
            this.#cellHandlers.td.appendChild(this.#cellHandlers.outerDiv);
            this.#cellHandlers.outerDiv = null;
            this.focus();
        },
        _blur: e => {
            this.#cellHandlers.endEdit(e);
        },
        _input: e => {
            return this.#cellHandlers.filterKey(e);
        },
        _keydown: e => {
            e.stopPropagation();
            switch (e.key) {
                case 'Escape':
                    this.#cellHandlers.commitChanges = false;
                    this.#cellHandlers.endEdit();
                    break;
                case 'Tab':
                case 'Enter':
                    this.#cellHandlers.input.removeEventListener('blur', this.#cellHandlers._blur);
                    if (e.key == 'Tab') {
                        let index = this.#cellHandlers.lvCell.columnIndex + 1;
                        const cells = this.#cellHandlers.td.parentElement.cells;
                        if (index == cells.length)
                            index = 0;
                        this.#setActiveCell(cells[index]);
                    }
                    this.#cellHandlers.commitChanges = true;
                    this.#cellHandlers.endEdit();
                    break;
            }
        }
    }

#cellHandlers is a property of a class that subclasses HTMLElement. It acts/mimics some capabilities of c#’s DataGridView. When a cell is clicked in this class and the cell’s editable property is true, #cellHandlers.beginEdit is invoked where input element is created when it has not been created yet and added to the cell. The cell is a table cell (td) stored in #cellHandlers.td. Upon hooking up the events and setting focus on the input, blur event fires immediately and the handler of this event calls #cellHandlers.endEdit which, obviously end the editing state of the cell. Why such a behavior and how can I prevent it?

BTW, I hope no one suggests jQuery-ish solution although it could be a last resort.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật