Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
110 views
in Technique[技术] by (71.8m points)

javascript - One of my query selector is preventing another from working

I have two VanilaJs click addEventLister for my delete button. two of them are doing what I want them to do but the problem is, only the one on top running but the one below does not run. a.deleteprogram is preventing a.deleteevent because it's above a.deleteevent

I mean the one on top prevents the one below from running. when I switch them they do the same thing to each other. what could be the cause? how do I set my code so that both can run without preventing the other?

 const trashcan = document.querySelector("a.deleteprogram");
 trashcan.addEventListener("click", (e) => {
   const endpoint = `/programs/${trashcan.dataset.doc}`;
     fetch(endpoint, {
       method: "DELETE",
       })
      .then((response) => response.json())
      .then((data) => (window.location.href = data.redirect))
      .catch((error) => console.log(error));
    });




  const trashcan2 = document.querySelector("a.deleteevent");
  trashcan2.addEventListener("click", (e) => {
   const endpoint = `/events/${trashcan2.dataset.doc}`;
     fetch(endpoint, {
      method: "DELETE",
     })
    .then((response) => response.json())
    .then((data) => (window.location.href = data.redirect))
    .catch((error) => console.log(error));
   });

this is my event link <a class="deleteevent" href="" data-doc="<%=event._id%>">Delete</a> and this is my program link <a class="deleteprogram" href="#" data-doc="<%= program._id%>">Delete</a>

question from:https://stackoverflow.com/questions/65850331/one-of-my-query-selector-is-preventing-another-from-working

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...