1

I am using jquery on a contenteditable div, I have an element in this div which has a certain class, I want to check if the user is about to delete that element from the div, and then delete an element with a matching class.

it will be like this

<span class='deletable-1'></span> wew ewe w ew  <span class='deletable-1'></span>

I want to check when the user is about to delete deletable-1 and delete the matching span,

How can I do this using jQuery ?

engma
  • 1,849
  • 2
  • 26
  • 55

1 Answers1

0

I'm not sure if I undersatnd your question, but it seems that when something is removed, you want to delete other elements witht he same class. Is that correct? If so, you would want to assign ALL such elements one class (to bind the events) - and a separate class (to match to their partners). Like so:

<span class='RemoveClass deletable-1'></span> wew ewe w ew  <span class='RemoveClass deletable-1'></span>
<span class='RemoveClass deletable-2'></span> wew ewe w ew  <span class='RemoveClass deletable-2'></span>

$(".RemoveClass").on("remove", function(){
     var classList = $(this).className.split(/\s+/);
     for (var i = 0; i < classList.length; i++) {
          if (classList[i] != 'RemoveClass') //You can come up with better logic for determining if this is the right class, but this is just an example
               $("." + classList[i]).each(function(){$(this).remove();});
     }
});
Stan Shaw
  • 3,014
  • 1
  • 12
  • 27
  • For the record this didn't answer the OP. – Mike Purcell Mar 30 '16 at 14:36
  • @MikePurcell How? He said he wanted to check if a user is about to delete a span - and then delete the span with the matching class with jQuery. Furthermore, he accepted the answer without asking for clarification or rephrasing his question, so your comment has me a bit perplexed, here. – Stan Shaw Mar 30 '16 at 17:57
  • Title of op: "get the deleted element from backspace event", your answer deletes multiple elements based on same class. When I was googling around for how to get last element, and only last element deleted this came up as a potential answer. If OP found this answer useful then title should be changed... – Mike Purcell Apr 13 '16 at 17:14
  • From the OP: " I want to check if the user is about to delete that element from the div, and then delete an element with a matching class." I felt it was important to answer the question that he asked; providing a solution to his problem was my objective. The answer was accepted and upvoted, so it appears that the person asking the question feels the same way. You're correct that the title of the question should be rephrased for clarity, but I disagree that "this didn't answer the OP", which was your initial assertion. Did you actually read his question and code? – Stan Shaw Apr 14 '16 at 13:14
  • Sure did, read the title and the question, he wanted to delete a single element, your example ASSUMES multiple elements. – Mike Purcell Apr 19 '16 at 20:03
  • My solution does not assume multiple elements, but it works for a single element AND multiple elements. Do you argue for the sake of arguing or am I missing something? – Stan Shaw Apr 20 '16 at 12:43