i’ve been having a problem where i have a form in which several entities within an unordered list are displayed, a name and a button dinamically with vanilla javascript. The problem is, im changing the action value from the html form, from javascript this so i can add an id that i get in a JSON from a http request so i can manipulate a specific object, but it doesnt send the “delete” request, which is actually a post request that im managing from my server side correctly (tested in postman and works like a charm), but when calling from the front-end it stops working. Here are my html and script.
PANEL.HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<!-- <meta http-equiv="Content-Security-Policy" content="default-src https://cdn.example.net; child-src 'none'; object-src 'none'"> -->
<link rel="stylesheet" type="text/css" href="css/panel-style.css">
<script defer src="panel-script.js"></script>
<title>Panel de Control</title>
</head>
<body>
<header>
<img src="assets/mrvapeslogo.png" alt="mrvapes logo">
<a href="index.html"></a>
</header>
<section>
<form id="files-form" accept-charset="UTF-8" enctype="multipart/form-data" action="#" autocomplete="off" method="POST" onsubmit="return validate(this);">
<label for="user">Banners Activos</label><br/>
<ul id="files-container">
</ul>
</form>
<form accept-charset="UTF-8" enctype="multipart/form-data" action="http://localhost:3000/banner" autocomplete="off" method="POST" target="_blank">
<div class="container">
<div class="button-wrap">
<!-- <label class="button" for="upload">Subir</label> -->
<input type="text" id="name" placeholder="Nombre de Archivo" value="" name="name" required>
<input id="image" name="file" value="Subir" placeholder="Subir Archivo" type="file" required>
<button id="upload" value="post-request" type="submit">Enviar</button>
<!-- <input id="upload" type=" submit " value="Enviar " onclick="submit() "> -->
</div>
</div>
</form>
</section>
</body>
PANEL-SCRIPT.JS
const list = document.getElementById("files-container");
const filesForm = document.getElementById("files-form");
fetch('http://localhost:3000/banner')
.then(response => response.json())
.then(json => {
console.log(json)
console.log(json.message)
for (let i = 0; i < json.message.length; i++) {
var item = `<li>${json.message[i].name}
<button id="delete-button" onclick="deleteButton('${json.message[i].name}','${json.message[i]._id}')">Borrar</button>
</li>`;
list.innerHTML += item;
}
});
function deleteButton(name, id) {
var answer = confirm(`Seguro que deseas borrar ${name}?`);
if (answer) {
filesForm.action = `http://localhost:3000/banner/${id}`;
alert('Borrado Correctamente!');
window.location.reload(true);
} else {
validate();
}
}
function validate() {
return false
}
HAPPY HOLIDAYS! 😀