How to bind a function when an element change/mutate #2161
Answered
by
PierreQuentel
BobSquarePants
asked this question in
Q&A
-
Hi everyone, I would like to bind a function when an element (table) change / mutate (actually the innerHTML) I saw this "issues" #1851 but the example is too complex.. from browser import document, window, html
def f(mutations, obserser):
for mutation in mutations:
for addedNode in mutation.addedNodes:
checkNode(addedNode)
observer = window.MutationObserver.new(f)
def checkNode(addedNode):
print('added node', addedNode)
observer.observe(document.documentElement, {
'childList': True,
'subtree': True
})
# Add an element
document <= html.H1("Hello world !") isn't there something more simpler for what I would like to achieve ? |
Beta Was this translation helpful? Give feedback.
Answered by
PierreQuentel
Feb 20, 2023
Replies: 1 comment 2 replies
-
If a 10 line almost self-explanatory example is too complex, I don't know what is not complex... Here is this example adapted to adding a button and binding it: from browser import document, window, html
def f(mutations, observer):
for mutation in mutations:
for addedNode in mutation.addedNodes:
checkNode(addedNode)
observer = window.MutationObserver.new(f)
def click(ev):
print('clicked !')
def checkNode(addedNode):
if addedNode.tagName == 'BUTTON':
addedNode.bind('click', click)
observer.observe(document.documentElement, {
'childList': True,
'subtree': True
})
# Add an element
document <= html.BUTTON("Click") |
Beta Was this translation helpful? Give feedback.
2 replies
Answer selected by
BobSquarePants
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
If a 10 line almost self-explanatory example is too complex, I don't know what is not complex...
Here is this example adapted to adding a button and binding it: