Child element count of parent element in DOM change

1

A div receives a new paragraph with every click, jQuery checks if there was a change in the DOM, the event is usually called but is returning the message:

UNDEFINED
. Verification does not work with .size () and .lenght of jQuery v3.3.1 .

HTML:

<div class='mensagem-list'></div>

And jQuery:

<script>
    $("body").on('DOMSubtreeModified', '.mensagem-list', function() {

        window.alert( $('.p-list').size );

    });
</script>

On the console every time you click the button, the date looks like this:

<div class='mensagem-list'>
    <p class='p-list'>[mensagem exibida]</p>
    <p class='p-list'>[mensagem exibida]</p>
</div>
    
asked by anonymous 30.08.2018 / 23:18

2 answers

1

This would be what you would like to tell the child elements of the div:

$(document.body).on("click", function() {
    $(".mensagem-list").append($("<p>Um novo parágrafo!</p>"));
    var n = $("p").length;
    console.log(n + ' elementos');
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<div class='mensagem-list'>
   <p>Um novo parágrafo!</p>
</div>
    
31.08.2018 / 00:27
0

The DOMSubtreeModified event is deprecated.

What you can do is take this count in the click event, where you are inserting the p tag

    
30.08.2018 / 23:21