Add texts.js for AJAX load and form submission
This commit is contained in:
parent
822176fcd3
commit
4c919d2205
1 changed files with 28 additions and 0 deletions
28
public/js/texts.js
Normal file
28
public/js/texts.js
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
document.addEventListener('DOMContentLoaded', () => {
|
||||
const textsList = document.getElementById('texts-list');
|
||||
const form = document.getElementById('texts-form');
|
||||
|
||||
async function loadTexts() {
|
||||
const res = await fetch('/api/texts');
|
||||
const texts = await res.json();
|
||||
textsList.innerHTML = texts.map(text => '<li>' + text.name + '</li>').join('');
|
||||
}
|
||||
|
||||
form.addEventListener('submit', async (e) => {
|
||||
e.preventDefault();
|
||||
const formData = new FormData(form);
|
||||
const res = await fetch('/api/texts', {
|
||||
method: 'POST',
|
||||
body: formData,
|
||||
});
|
||||
if (res.ok) {
|
||||
const text = await res.json();
|
||||
const li = document.createElement('li');
|
||||
li.textContent = text.name;
|
||||
textsList.appendChild(li);
|
||||
form.reset();
|
||||
}
|
||||
});
|
||||
|
||||
loadTexts();
|
||||
});
|
||||
Loading…
Add table
Add a link
Reference in a new issue