how would I make a delete button work on a website

I am currently working on a javascript project with HTMl, CSS and Vue. The Project is to make a cookbook online that you can add your own recipes, and also delete recipes from. The issue that I have is that when I try to implement a delete button for the added recipes, it breaks the website so that it doesn’t show the given recipes anymore and I cant add any more new recipes either. I am using this as a baseline for the delete code:

which makes the button work but it does not delete anything

I have tried changed the base recipe to a ref and the function to add a new recipe to a ref as well but then I have the issue of the base recipe not showing up anymore and the button for adding a new recipe either. So now I do not know how to change my code for both the add recipe button and the delete button to work.

Below is my code and I have changed it back to before the ref changes so that at least the add recipe button works:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>import {loadThings} from "@/components/script.js";
import { ref } from 'vue';
let showRecipeName = true;
let showForm = ref(false);
let recipes = [
{
name: "Spaghetti Carbonara",
picture:
preparationTime: "20 Minuten",
cookingTime: "15 Minuten",
description: "Spaghetti Carbonara ist ein klassisches italienisches Nudelgericht, das in der Regel aus Spaghetti, Guanciale, Ei, Pecorino Romano und Pfeffer zubereitet wird. Die Zutaten werden in einer Pfanne gemischt, bis sie eine cremige Konsistenz haben. Das Gericht wird oft mit zusätzlichem Pecorino Romano und Pfeffer garniert.",
ingredients: [
"400g Spaghetti",
"200g Guanciale",
"4 Eier",
"100g Pecorino Romano",
"Pfeffer"
],
instructions: [
"Die Spaghetti in einem großen Topf mit kochendem Salzwasser al dente kochen.",
"In der Zwischenzeit das Guanciale in einer Pfanne bei mittlerer Hitze knusprig braten.",
"Die Eier in einer Schüssel verquirlen und den geriebenen Pecorino Romano hinzufügen.",
"Die gekochten Spaghetti abgießen und in die Pfanne mit dem Guanciale geben. Die Ei-Käse-Mischung darüber gießen und gut vermengen.",
"Mit Pfeffer würzen und sofort servieren."
]
}
];
let newRecipe ={
name: "",
picture: "",
preparationTime: "",
cookingTime: "",
description: "",
ingredients: [""],
instructions: [""]
};
function addRecipe() {
newRecipe.ingredients = newRecipe.ingredients.split('n');
newRecipe.instructions = newRecipe.instructions.split('n');
recipes.push({...newRecipe});
newRecipe = {
name: "",
preparationTime: "",
cookingTime: "",
description: "",
ingredients: [""],
instructions: [""]
};
showForm.value = false;
}
function showAddRecipeForm() {
showForm.value = true;
}
function deleteRecipe(index) {
console.log("deleteRecipe function called with index: ", index);
recipes.splice(index, 1);
}
</script>
<template>
<div class="recipeheader">
<h1>Rezepte</h1>
<button @click="loadThings">Load recipes</button>
<button @click="showAddRecipeForm">Add Recipe</button>
<div class="addrecipe">
<form @submit.prevent="addRecipe" v-if="showForm">
<label>Name: <input v-model="newRecipe.name" required /></label>
<label>Preparation Time: <input v-model="newRecipe.preparationTime" required /></label>
<label>Cooking Time: <input v-model="newRecipe.cookingTime" required /></label>
<label>Description: <input v-model="newRecipe.description" required /></label>
<label>Ingredients: <textarea v-model="newRecipe.ingredients" required></textarea></label>
<label>Instructions: <textarea v-model="newRecipe.instructions" required></textarea></label>
<label>Picture: <input v-model="newRecipe.picture" /></label>
<button type="submit">Add Recipe</button>
</form>
</div>
</div>
<div class="newRecipe" v-for="(recipe, index) in recipes" :key="index">
<h2>{{ recipe.name }}</h2>
<button @click="deleteRecipe(index)">Delete Recipe</button>
<img :src="recipe.picture" alt="Picture of the recipe" />
<h3>Description</h3>
<p>{{ recipe.description }}</p>
<h3>Preparation Time</h3>
<p>{{ recipe.preparationTime }}</p>
<h3>Cooking Time</h3>
<p>{{ recipe.cookingTime }}</p>
<h3>Ingredients</h3>
<ul>
<li v-for="(ingredient, index) in recipe.ingredients" :key="index">{{ ingredient }}</li>
</ul>
<h3>Instructions</h3>
<ul>
<li v-for="(instruction, index) in recipe.instructions" :key="index">{{ instruction }}</li>
</ul>
</div>
</template>
<style>
.addrecipe
{
display: flex;
flex-direction: column;
align-items: flex-start;
gap: 1rem;
margin-top: 3rem;
padding: 50px;
}
.addrecipe button[type="submit"] {
margin-top: 20px;
align-items: flex-start;
}
.body {
margin-top: 0rem;
display: flex;
flex-direction: column;
align-items: flex-start; /* Adjust this line */
gap: 2px; /* Adjust as needed */
background-color: whitesmoke;
}
.newRecipe{
margin-top: 0rem;
display: flex;
flex-direction: column;
align-items: flex-start; /* Adjust this line */
gap: 20px; /* Adjust as needed */
background-color: whitesmoke;
padding: 10px;
}
.recipeheader {
min-height: 30vh;
display: flex;
align-items: center;
justify-content: center;
flex-direction: column;
background-color: white;
}
@media (min-width: 1024px) {
.recipeheader {
height: 100vh;
justify-content: center;
display: flex;
align-items: center;
}
}
</style>
</code>
<code>import {loadThings} from "@/components/script.js"; import { ref } from 'vue'; let showRecipeName = true; let showForm = ref(false); let recipes = [ { name: "Spaghetti Carbonara", picture: preparationTime: "20 Minuten", cookingTime: "15 Minuten", description: "Spaghetti Carbonara ist ein klassisches italienisches Nudelgericht, das in der Regel aus Spaghetti, Guanciale, Ei, Pecorino Romano und Pfeffer zubereitet wird. Die Zutaten werden in einer Pfanne gemischt, bis sie eine cremige Konsistenz haben. Das Gericht wird oft mit zusätzlichem Pecorino Romano und Pfeffer garniert.", ingredients: [ "400g Spaghetti", "200g Guanciale", "4 Eier", "100g Pecorino Romano", "Pfeffer" ], instructions: [ "Die Spaghetti in einem großen Topf mit kochendem Salzwasser al dente kochen.", "In der Zwischenzeit das Guanciale in einer Pfanne bei mittlerer Hitze knusprig braten.", "Die Eier in einer Schüssel verquirlen und den geriebenen Pecorino Romano hinzufügen.", "Die gekochten Spaghetti abgießen und in die Pfanne mit dem Guanciale geben. Die Ei-Käse-Mischung darüber gießen und gut vermengen.", "Mit Pfeffer würzen und sofort servieren." ] } ]; let newRecipe ={ name: "", picture: "", preparationTime: "", cookingTime: "", description: "", ingredients: [""], instructions: [""] }; function addRecipe() { newRecipe.ingredients = newRecipe.ingredients.split('n'); newRecipe.instructions = newRecipe.instructions.split('n'); recipes.push({...newRecipe}); newRecipe = { name: "", preparationTime: "", cookingTime: "", description: "", ingredients: [""], instructions: [""] }; showForm.value = false; } function showAddRecipeForm() { showForm.value = true; } function deleteRecipe(index) { console.log("deleteRecipe function called with index: ", index); recipes.splice(index, 1); } </script> <template> <div class="recipeheader"> <h1>Rezepte</h1> <button @click="loadThings">Load recipes</button> <button @click="showAddRecipeForm">Add Recipe</button> <div class="addrecipe"> <form @submit.prevent="addRecipe" v-if="showForm"> <label>Name: <input v-model="newRecipe.name" required /></label> <label>Preparation Time: <input v-model="newRecipe.preparationTime" required /></label> <label>Cooking Time: <input v-model="newRecipe.cookingTime" required /></label> <label>Description: <input v-model="newRecipe.description" required /></label> <label>Ingredients: <textarea v-model="newRecipe.ingredients" required></textarea></label> <label>Instructions: <textarea v-model="newRecipe.instructions" required></textarea></label> <label>Picture: <input v-model="newRecipe.picture" /></label> <button type="submit">Add Recipe</button> </form> </div> </div> <div class="newRecipe" v-for="(recipe, index) in recipes" :key="index"> <h2>{{ recipe.name }}</h2> <button @click="deleteRecipe(index)">Delete Recipe</button> <img :src="recipe.picture" alt="Picture of the recipe" /> <h3>Description</h3> <p>{{ recipe.description }}</p> <h3>Preparation Time</h3> <p>{{ recipe.preparationTime }}</p> <h3>Cooking Time</h3> <p>{{ recipe.cookingTime }}</p> <h3>Ingredients</h3> <ul> <li v-for="(ingredient, index) in recipe.ingredients" :key="index">{{ ingredient }}</li> </ul> <h3>Instructions</h3> <ul> <li v-for="(instruction, index) in recipe.instructions" :key="index">{{ instruction }}</li> </ul> </div> </template> <style> .addrecipe { display: flex; flex-direction: column; align-items: flex-start; gap: 1rem; margin-top: 3rem; padding: 50px; } .addrecipe button[type="submit"] { margin-top: 20px; align-items: flex-start; } .body { margin-top: 0rem; display: flex; flex-direction: column; align-items: flex-start; /* Adjust this line */ gap: 2px; /* Adjust as needed */ background-color: whitesmoke; } .newRecipe{ margin-top: 0rem; display: flex; flex-direction: column; align-items: flex-start; /* Adjust this line */ gap: 20px; /* Adjust as needed */ background-color: whitesmoke; padding: 10px; } .recipeheader { min-height: 30vh; display: flex; align-items: center; justify-content: center; flex-direction: column; background-color: white; } @media (min-width: 1024px) { .recipeheader { height: 100vh; justify-content: center; display: flex; align-items: center; } } </style> </code>
import {loadThings} from "@/components/script.js";
import { ref } from 'vue';
let showRecipeName = true;
let showForm = ref(false);
let recipes =  [
  {
    name: "Spaghetti Carbonara",
    picture: 
    preparationTime: "20 Minuten",
    cookingTime: "15 Minuten",
    description: "Spaghetti Carbonara ist ein klassisches italienisches Nudelgericht, das in der Regel aus Spaghetti, Guanciale, Ei, Pecorino Romano und Pfeffer zubereitet wird. Die Zutaten werden in einer Pfanne gemischt, bis sie eine cremige Konsistenz haben. Das Gericht wird oft mit zusätzlichem Pecorino Romano und Pfeffer garniert.",
    ingredients: [
      "400g Spaghetti",
      "200g Guanciale",
      "4 Eier",
      "100g Pecorino Romano",
      "Pfeffer"
    ],
    instructions: [
      "Die Spaghetti in einem großen Topf mit kochendem Salzwasser al dente kochen.",
      "In der Zwischenzeit das Guanciale in einer Pfanne bei mittlerer Hitze knusprig braten.",
      "Die Eier in einer Schüssel verquirlen und den geriebenen Pecorino Romano hinzufügen.",
      "Die gekochten Spaghetti abgießen und in die Pfanne mit dem Guanciale geben. Die Ei-Käse-Mischung darüber gießen und gut vermengen.",
      "Mit Pfeffer würzen und sofort servieren."
    ]
  }
];
let newRecipe ={
  name: "",
  picture: "",
  preparationTime: "",
  cookingTime: "",
  description: "",
  ingredients: [""],
  instructions: [""]
};

function addRecipe() {
  newRecipe.ingredients = newRecipe.ingredients.split('n');
  newRecipe.instructions = newRecipe.instructions.split('n');
  recipes.push({...newRecipe});
  newRecipe = {
    name: "",
    preparationTime: "",
    cookingTime: "",
    description: "",
    ingredients: [""],
    instructions: [""]
  };
  showForm.value = false;
}
function showAddRecipeForm() {
  showForm.value = true;
}
function deleteRecipe(index) {
  console.log("deleteRecipe function called with index: ", index);
  recipes.splice(index, 1);
}



</script>

<template>
  <div class="recipeheader">
    <h1>Rezepte</h1>
    <button @click="loadThings">Load recipes</button>
    <button @click="showAddRecipeForm">Add Recipe</button>
    <div class="addrecipe">
      <form @submit.prevent="addRecipe" v-if="showForm">
        <label>Name: <input v-model="newRecipe.name" required /></label>
        <label>Preparation Time: <input v-model="newRecipe.preparationTime" required /></label>
        <label>Cooking Time: <input v-model="newRecipe.cookingTime" required /></label>
        <label>Description: <input v-model="newRecipe.description" required /></label>
        <label>Ingredients: <textarea v-model="newRecipe.ingredients" required></textarea></label>
        <label>Instructions: <textarea v-model="newRecipe.instructions" required></textarea></label>
        <label>Picture: <input v-model="newRecipe.picture"  /></label>
        <button type="submit">Add Recipe</button>
      </form>
    </div>
  </div>
  <div class="newRecipe" v-for="(recipe, index) in recipes" :key="index">
    <h2>{{ recipe.name }}</h2>
    <button @click="deleteRecipe(index)">Delete Recipe</button>
    <img :src="recipe.picture" alt="Picture of the recipe" />
    <h3>Description</h3>
    <p>{{ recipe.description }}</p>
    <h3>Preparation Time</h3>
    <p>{{ recipe.preparationTime }}</p>
    <h3>Cooking Time</h3>
    <p>{{ recipe.cookingTime }}</p>
    <h3>Ingredients</h3>
    <ul>
      <li v-for="(ingredient, index) in recipe.ingredients" :key="index">{{ ingredient }}</li>
    </ul>
    <h3>Instructions</h3>
    <ul>
      <li v-for="(instruction, index) in recipe.instructions" :key="index">{{ instruction }}</li>
    </ul>
  </div>




</template>

<style>
.addrecipe
{
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 1rem;
  margin-top: 3rem;
  padding: 50px;
}
.addrecipe button[type="submit"] {
  margin-top: 20px;
  align-items: flex-start;
}
.body {
  margin-top: 0rem;
  display: flex;
  flex-direction: column;
  align-items: flex-start; /* Adjust this line */
  gap: 2px; /* Adjust as needed */
  background-color: whitesmoke;

}
.newRecipe{
  margin-top: 0rem;
  display: flex;
  flex-direction: column;
  align-items: flex-start; /* Adjust this line */
  gap: 20px; /* Adjust as needed */
  background-color: whitesmoke;
  padding: 10px;
}
.recipeheader {
    min-height: 30vh;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-direction: column;
    background-color: white;
}
@media (min-width: 1024px) {
    .recipeheader {
        height: 100vh;
      justify-content: center;
        display: flex;
        align-items: center;
    }
}
</style>

New contributor

Alexander von Allwörden is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

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