MutationObserver

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

Louise Swift
No Mayo
2 min readJul 24, 2017

--

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.

View this simple example’s code here.

Current browser support:

Full syntax & usage info:

--

--

Louise Swift
No Mayo

Software engineer at FutureLearn. Mathematics BSc student.