Answer the question
In order to leave comments, you need to log in
What event should be set to watch the change in the number of elements?
Good day, gentlemen. Faced such a problem. Hung an event
.change()
on the span element. There are elements inside this span. When I add elements "inside" - the event works well, and does what I need. But when I remove the element "from the inside", the previous value is returned to me, not the current one. For example inside I have 4 elements. When I add one, it returns a new amount to me - tobish 5. But when I remove an element, the same value is returned to me, not one less.
Help, please, how can I be. It is necessary to count the number of elements inside, and when its number changes, send the data to the server. Now it turns out that when adding, what is needed is sent, but when I delete an element, the unchanged value is sent.
Thank you.
$('#id_set_products-wrapper').change(function () {
var lst = [];
$.each($('#id_set_products-deck')[0].childNodes, function (key, value) {
lst.push(value.dataset.value)
});
})
Answer the question
In order to leave comments, you need to log in
An example on jsfiddle to the studio :) You need to watch how you delete and how you count the number of elements.
To start with, spans don't have a change event by default, unlike form elements.
You can use something from DOM Mutation Events
For example DOMSubtreeModified
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question