Skip to content

How to bind a function when an element change/mutate #2161

Discussion options

You must be logged in to vote

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")

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@BobSquarePants
Comment options

@BobSquarePants
Comment options

Answer selected by BobSquarePants
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants