submit add child form on enter key

extract the save-child handler into a submit closure shared by
the save button click and a keydown listener on the input. also
focus the input as soon as the form opens so the user can type
and hit enter without touching the mouse.
This commit is contained in:
Yisroel Baum 2026-05-01 11:53:54 +03:00
parent 74705379cb
commit 3928fef213
Signed by: yisroelbaum
GPG key ID: 0FA60884F75520A9

View file

@ -117,7 +117,8 @@ function toggleAddForm(li, parentNodeId, textId) {
const saveBtn = document.createElement('button');
saveBtn.textContent = 'Save';
saveBtn.className = 'save-child';
saveBtn.addEventListener('click', () => {
function submit() {
const title = input.value.trim();
if (!title) return;
@ -133,10 +134,19 @@ function toggleAddForm(li, parentNodeId, textId) {
return res.json();
})
.then(() => fetchAndRenderNodes(textId));
}
saveBtn.addEventListener('click', submit);
input.addEventListener('keydown', (event) => {
if (event.key === 'Enter') {
event.preventDefault();
submit();
}
});
li.appendChild(input);
li.appendChild(saveBtn);
input.focus();
}
function toggleBulkAddForm(li, parentNodeId, textId) {