+ 2

Call a function, when a new element gets created

I want to know how many "p" elements exist in this document and want to call a function, when another one gets appended to the body. when i do the qurtySelectorAll("p").length Method, i only know how many elements are existing right at the moment when i do the Method. But then, an other element gets created the number upon is wrong. I want to call a function when a new this number changes. Please ask, if you do not understand something in this question https://code.sololearn.com/Wi5BeYtoirGR/?ref=app

7th May 2018, 1:33 PM
Sebay
Sebay - avatar
7 Antworten
0
Hi Sebay query selector returns a static nodelist. You want to use getElement by Class/tagname /name which returns a live nodelist, and is kept updated.
7th May 2018, 8:03 PM
Mike Choy
Mike Choy - avatar
+ 4
Make a counter variable that goes up by 1 everytime you make a new p element
7th May 2018, 1:35 PM
ReimarPB
ReimarPB - avatar
+ 1
Sebay document.getElementsByTagName("p").length should return the number of p tags
7th May 2018, 2:46 PM
ReimarPB
ReimarPB - avatar
+ 1
Thx @Mike Choy Worx
8th May 2018, 5:20 AM
Sebay
Sebay - avatar
0
Call your other function inside the elem function
7th May 2018, 1:37 PM
TurtleShell
TurtleShell - avatar
0
Both ideas are good, but i can not edit the given code So i am not able to insert a counter, neither a function inside the elem function This is just an example which should make it easier to understand the question
7th May 2018, 2:36 PM
Sebay
Sebay - avatar
0
but then some new elements appear and again i do not got the correct number of p elements
7th May 2018, 2:48 PM
Sebay
Sebay - avatar