MutationObserver

1 min read Original article ↗

Sounds like an evil villain’s superpower, but actually could be a helpful sidekick in your Rails app.

Louise Swift

Press enter or click to view image in full size

In a Rails app, you sometimes let users add extra fields to a create/edit form, e.g. when CRUD-ing multiple child records within the parent record’s view.

And sometimes you might want something in the view to change as a result.

And you might not be using jQuery, because perhaps you don’t need a whole library for one or two small JS things.

In this particular situation, MutationObserver could help!

When you set a MutationObserver on a target element, that element can watch for changes to its attributes, data and child nodes and respond as instructed.

Press enter or click to view image in full size

View this simple example’s code here.

Current browser support:

Full syntax & usage info: