MutationObserver

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

Louise Swift
Jul 24, 2017 · 2 min read

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:

No Mayo

No Mayo are a London based digital agency.

Louise Swift

Written by

Software developer. Mathematics student. Working on Curriculum-App.io, too.

No Mayo

No Mayo

No Mayo are a London based digital agency.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade