3

I've got the following situation: I'm storing dates in text-inputs and above those, the min- and maxdates are displayed. It is possible to add and remove additional inputs. The dates are stored in an array of objects [{id: 1, val: '15.3.13'}, {id: 2, val: '1.4.13'}...] The onchange-event for these inputs now checks the array and calculates the minimum and maximum dates for display on the top of the page. But if I add an input, make it the minimum/maximum date and then remove it, the onchange-event isn't called.

Is there some kind of onremove-event for jQuery or do I have to work my way around and call everything manually?

(The scenario above is a highly simplified version of the real problem, just to get the idea of what I'm doing. Please don't just say "Why don't you call the onchange-method in the onclick-method of the button that removes the inputs?", because it's just not that easy ;-))

Vince
  • 1,517
  • 2
  • 18
  • 43
  • You should create a class/interface for your object, which handles such things for you. There are no `listeners` or `handlers` for native JavaScript objects. – Zeta Apr 09 '13 at 04:44
  • I'm not trying to add a handler to the array itself, but to the DOM element (input) so that the input itself can delete the matching entry from the array. Sorry for the misunderstanding. I already found a solution for my specific problem now, but it would be very interesting to know if there was such a thing as a "onremove"-event or something similar in jQuery though. – Vince Apr 09 '13 at 05:03
  • bind unbind? could you be looking for? – Four_lo Apr 09 '13 at 05:21
  • Nope, I'm not looking for unbinding events, I'm looking for an event that is triggered when the specified DOM-element is removed from the page. – Vince Apr 09 '13 at 05:29
  • there is a `DOMNodeRemoved` event, but browser support isn't wide. – goat Jun 30 '13 at 19:15

2 Answers2

1

Are you looking for this snippet? jQuery - Trigger event when an element is removed from the DOM
If not, please provide some code.

Community
  • 1
  • 1
Alexander Scholz
  • 2,100
  • 1
  • 20
  • 35
-3

You should use jquery's $.when() and .then()

    $.when($('#div').remove()).then(doAfterRemove());

Check out this Fiddle to see it working to see it working.

Adrian Trainor
  • 267
  • 1
  • 9
  • 2
    this immediately removes the element, and immediately calls the doAfterRemove function. – goat Jun 30 '13 at 19:08
  • 1
    I know. That is what Vince asked for. He said "I'm looking for an event that is triggered when the specified DOM-element is removed from the page." The doAfterRemove function can be passed an id which can then be used to update the array. – Adrian Trainor Jun 30 '13 at 19:23
  • my point is that your code behaves identical to: `$('#div').remove(); doAfterRemove();` and such isn't a solution. – goat Jun 30 '13 at 19:51
  • ok, i see your point now, my bad. I think you were right about the DOMNodeRemoved event, and I found a blog describing how to use this and improve browser support [here](http://www.bennadel.com/blog/1623-Ask-Ben-Detecting-When-DOM-Elements-Have-Been-Removed-With-jQuery.htm). – Adrian Trainor Jun 30 '13 at 20:17