{"id":24229,"date":"2015-07-02T21:01:52","date_gmt":"2015-07-02T13:01:52","guid":{"rendered":"https:\/\/www.hongkiat.com\/blog\/?p=24229"},"modified":"2024-07-30T19:21:16","modified_gmt":"2024-07-30T11:21:16","slug":"mutationobserver-api","status":"publish","type":"post","link":"https:\/\/www.hongkiat.com\/blog\/mutationobserver-api\/","title":{"rendered":"How to Use the MutationObserver API for Tracking DOM Changes"},"content":{"rendered":"<p>Imagine this scenario: Rita, a magazine writer, is editing an article online. She saves her changes and sees the message \u201cChanges saved!\u201d Just then, she notices a typo she missed. She fixes it and is about to click \u201csave\u201d when she gets an angry phone call from her boss.<\/p>\n<p>After the call, she turns back to the screen, sees \u201cChanges saved!\u201d shuts down her computer, and storms out of the office.<\/p>\n<p>Apart from my poor story-telling skills, we can see from this short scenario the trouble that persistent message caused. To avoid this in the future, we should use a message that either prompts the user to acknowledge by clicking it or <strong>vanishes on its own<\/strong>. Using the latter for quick messages is a good idea.<\/p>\n<p>We already know how to make an element disappear from a page, so that shouldn\u2019t be a problem. What we need to know is <strong>when it appeared<\/strong>, so we can make it disappear after a reasonable time.<\/p>\n<h2>MutationObserver API<\/h2>\n<p>When a DOM element (like a message <code>div<\/code>) or any other node changes, we should be able to detect it. For a long time, developers had to rely on hacks and frameworks due to the lack of a native API. But that has changed.<\/p>\n<p>We now have <strong>MutationObserver<\/strong> (previously Mutation Events). <code>MutationObserver<\/code> is a JavaScript native object with a set of properties and methods. It lets us <strong>observe changes on any node<\/strong>, such as DOM Element, Document, Text, etc. By mutation, we mean <strong>the addition or removal of a node and changes to a node\u2019s attributes and data<\/strong>.<\/p>\n<p>Let\u2019s see an example for better understanding. We\u2019ll first set up where a message appears upon a button click, like the one Rita saw. Then we\u2019ll <strong>create and link a mutation observer to that message box<\/strong> and <strong>code the logic to auto-hide the message<\/strong>. Sound good?<\/p>\n<p><strong>Note<\/strong>: You might be thinking, \u201c<em>Why not just hide the message from inside the button click event itself in JavaScript?<\/em>\u201d In my example, I\u2019m not working with a server, so of course the client is responsible for showing the message and can hide it easily. But like in Rita\u2019s editing tool, if the server decides to change the DOM content, the client can only stay alert and wait.<\/p>\n<p>First, we create the setup to show the message on button click.<\/p>\n<pre>\r\n&lt;div id=\"msg\"&gt;&lt;\/div&gt;&lt;br \/&gt;\r\n&lt;button&gt;Show Message&lt;\/button&gt;\r\n<\/pre>\n<pre>\r\nvar msg = document.querySelector('#msg'),\r\n    SUCCESSMSG = \"Changes Saved!\";\r\n\r\n\/* Add button click event *\/\r\ndocument\r\n    .querySelector('button')\r\n    .addEventListener('click', showMsg);\r\n\r\nfunction showMsg() {\r\n    msg.textContent = SUCCESSMSG;\r\n    msg.style.background = 'teal';\r\n}\r\n<\/pre>\n<p>Once we have the initial setup running, let\u2019s do the following:<\/p>\n<ul>\n<li>Create a <code>MutationObserver<\/code> object with a user-defined callback function (the function is defined later in the post). The function will execute on every mutation observed by the <code>MutationObserver<\/code>.<\/li>\n<li>Create a config object to specify the kind of mutations to be observed by the <code>MutationObserver<\/code>.<\/li>\n<li>Bind the <code>MutationObserver<\/code> to the target, which is the \u2018msg\u2019 <code>div<\/code> in our example.<\/li>\n<\/ul>\n<pre>\r\n(function startObservation() {\r\n    \/\/ 1) Create a MutationObserver object\r\n    var observer = new MutationObserver(function(mutations) { \r\n        mutationObserverCallback(mutations);\r\n    });\r\n    \r\n    \/\/ 2) Create a config object\r\n    var config = {childList: true};\r\n    \r\n    \/\/ 3) Bind observer to the target\r\n    observer.observe(msg, config);\r\n})();\r\n<\/pre>\n<p>Below is a list of properties for the <code>config<\/code> object identifying the different kinds of mutations. Since in our example we only deal with a child text node created for the message text, we\u2019ve used the <code>childList<\/code> property.<\/p>\n<h2>Kinds of Mutations Observed<\/h2>\n<table>\n<thead>\n<tr>\n<td>Property<\/td>\n<td>When Set to <code>true<\/code><\/td>\n<\/tr>\n<\/thead>\n<tr>\n<td><strong>childList<\/strong><\/td>\n<td>Insertion and removal of the target\u2019s child nodes are observed.<\/td>\n<\/tr>\n<tr>\n<td><strong>attributes<\/strong><\/td>\n<td>Changes in the target\u2019s attributes are observed.<\/td>\n<\/tr>\n<tr>\n<td><strong>characterData<\/strong><\/td>\n<td>Changes in the target\u2019s data are observed.<\/td>\n<\/tr>\n<\/table>\n<p>Now, let\u2019s look at the callback function that gets executed on every observed mutation.<\/p>\n<pre>\r\nfunction mutationObserverCallback(mutations) {\r\n    \/\/ Grab the first mutation\r\n    var mutationRecord = mutations[0];\r\n\r\n    \/\/ If a child node was added, hide the msg after 2s\r\n    if (mutationRecord.addedNodes[0] !== undefined) {\r\n        setTimeout(hideMsg, 2000);\r\n    }\r\n}\r\n\r\nfunction hideMsg() {\r\n    msg.textContent = '';\r\n    msg.style.background = 'none';\r\n}\r\n<\/pre>\n<p>Since we\u2019re only adding a message to the <code>div<\/code>, we\u2019ll just grab the first mutation observed on it and check if a text node was inserted. If we have more than one change happening, we can loop through the <code>mutations<\/code> array.<\/p>\n<p>Every mutation in the <code>mutations<\/code> array is represented by the object <code>MutationRecord<\/code> with the following properties:<\/p>\n<h2>Properties of <em>MutationRecord<\/em><\/h2>\n<table>\n<thead>\n<tr>\n<td>Property<\/td>\n<td>Returns<\/td>\n<\/tr>\n<\/thead>\n<tr>\n<td><strong>addedNodes<\/strong><\/td>\n<td>Empty array or array of nodes added.<\/td>\n<\/tr>\n<tr>\n<td><strong>attributeName<\/strong><\/td>\n<td>Null or name of the attribute that was added, removed, or changed.<\/td>\n<\/tr>\n<tr>\n<td><strong>attributeNamespace<\/strong><\/td>\n<td>Null or namespace of the attribute that was added, removed, or changed.<\/td>\n<\/tr>\n<tr>\n<td><strong>nextSibling<\/strong><\/td>\n<td>Null or next sibling of the node that was added or removed.<\/td>\n<\/tr>\n<tr>\n<td><strong>oldValue<\/strong><\/td>\n<td>Null or previous value of the attribute or data changed.<\/td>\n<\/tr>\n<tr>\n<td><strong>previousSibling<\/strong><\/td>\n<td>Null or previous sibling of the node that was added or removed.<\/td>\n<\/tr>\n<tr>\n<td><strong>removedNodes<\/strong><\/td>\n<td>Empty array or array of nodes that were removed.<\/td>\n<\/tr>\n<tr>\n<td><strong>target<\/strong><\/td>\n<td>Node targeted by the <code>MutationObserver<\/code>.<\/td>\n<\/tr>\n<tr>\n<td><strong>type<\/strong><\/td>\n<td>Type of mutation observed.<\/td>\n<\/tr>\n<\/table>\n<p>And\u2026 that\u2019s it. We just have to put the code together for the final step.<\/p>\n<p><iframe height=\"257\" scrolling=\"no\" src=\"https:\/\/codepen.io\/\/rpsthecoder\/embed\/jPGbVe\/?height=257&theme-id=14457&default-tab=result\" frameborder=\"no\" allowtransparency=\"true\" allowfullscreen=\"true\" style=\"width: 100%;\"> <\/iframe><\/p>\n<h2>Browser Support<\/h2>\n<figure><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/assets.hongkiat.com\/uploads\/mutationobserver-api\/can-i-use.jpg\" alt=\"Can I use Mutation Observer Chart\" width=\"878\" height=\"401\"><\/figure>\n<div class=\"sue-icon-text su-image-caption\" data-url=\"\" data-target=\"self\" style=\"min-height:34px;padding-left:36px;color:#333333\">\n<div class=\"sue-icon-text-icon\" style=\"color:#333333;font-size:24px;width:24px;height:24px\"><i class=\"sui sui-photo\" style=\"font-size:24px;color:#333333\"><\/i><\/div>\n<div class=\"sue-icon-text-content su-u-trim\" style=\"color:#333333\"><a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/caniuse.com\/mutationobserver\">IMAGE: Can I Use.Web.<\/a><\/div>\n<div style=\"clear:both;height:0\"><\/div>\n<\/div>\n<h2>References<\/h2>\n<ul>\n<li><a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/www.w3.org\/TR\/dom\/#interface-mutationobserver\">W3C DOM4 Mutation Observer<\/a>. W3C. Web. 19 June 2015<\/li>\n<li><a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/developer.mozilla.org\/en\/docs\/Web\/API\/MutationObserver\">MutationObserver<\/a>. <i>Mozilla Developer Network<\/i>. Web. 19 June 2015.<\/li>\n<\/ul>","protected":false},"excerpt":{"rendered":"<p>Imagine this scenario: Rita, a magazine writer, is editing an article online. She saves her changes and sees the message \u201cChanges saved!\u201d Just then, she notices a typo she missed. She fixes it and is about to click \u201csave\u201d when she gets an angry phone call from her boss. After the call, she turns back&hellip;<\/p>\n","protected":false},"author":145,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[3392],"tags":[3554,4616],"topic":[4520],"class_list":["entry-content","is-maxi"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v22.8 (Yoast SEO v27.6) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>How to Use the MutationObserver API for Tracking DOM Changes - Hongkiat<\/title>\n<meta name=\"description\" content=\"Imagine this scenario: Rita, a magazine writer, is editing an article online. She saves her changes and sees the message &quot;Changes saved!&quot; Just then, she\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.hongkiat.com\/blog\/mutationobserver-api\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to Use the MutationObserver API for Tracking DOM Changes\" \/>\n<meta property=\"og:description\" content=\"Imagine this scenario: Rita, a magazine writer, is editing an article online. She saves her changes and sees the message &quot;Changes saved!&quot; Just then, she\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.hongkiat.com\/blog\/mutationobserver-api\/\" \/>\n<meta property=\"og:site_name\" content=\"Hongkiat\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/hongkiatcom\" \/>\n<meta property=\"article:published_time\" content=\"2015-07-02T13:01:52+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-07-30T11:21:16+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/assets.hongkiat.com\/uploads\/mutationobserver-api\/can-i-use.jpg\" \/>\n<meta name=\"author\" content=\"Preethi Ranjit\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@hongkiat\" \/>\n<meta name=\"twitter:site\" content=\"@hongkiat\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Preethi Ranjit\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/mutationobserver-api\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/mutationobserver-api\\\/\"},\"author\":{\"name\":\"Preethi Ranjit\",\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/#\\\/schema\\\/person\\\/e981676afae36d1ff5feb75094950ab3\"},\"headline\":\"How to Use the MutationObserver API for Tracking DOM Changes\",\"datePublished\":\"2015-07-02T13:01:52+00:00\",\"dateModified\":\"2024-07-30T11:21:16+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/mutationobserver-api\\\/\"},\"wordCount\":760,\"commentCount\":5,\"publisher\":{\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/mutationobserver-api\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/assets.hongkiat.com\\\/uploads\\\/mutationobserver-api\\\/can-i-use.jpg\",\"keywords\":[\"DOM\",\"NodeJS\"],\"articleSection\":[\"Coding\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/mutationobserver-api\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/mutationobserver-api\\\/\",\"url\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/mutationobserver-api\\\/\",\"name\":\"How to Use the MutationObserver API for Tracking DOM Changes - Hongkiat\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/mutationobserver-api\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/mutationobserver-api\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/assets.hongkiat.com\\\/uploads\\\/mutationobserver-api\\\/can-i-use.jpg\",\"datePublished\":\"2015-07-02T13:01:52+00:00\",\"dateModified\":\"2024-07-30T11:21:16+00:00\",\"description\":\"Imagine this scenario: Rita, a magazine writer, is editing an article online. She saves her changes and sees the message \\\"Changes saved!\\\" Just then, she\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/mutationobserver-api\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/mutationobserver-api\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/mutationobserver-api\\\/#primaryimage\",\"url\":\"https:\\\/\\\/assets.hongkiat.com\\\/uploads\\\/mutationobserver-api\\\/can-i-use.jpg\",\"contentUrl\":\"https:\\\/\\\/assets.hongkiat.com\\\/uploads\\\/mutationobserver-api\\\/can-i-use.jpg\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/mutationobserver-api\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How to Use the MutationObserver API for Tracking DOM Changes\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/\",\"name\":\"Hongkiat\",\"description\":\"Tech and Design Tips\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/#organization\",\"name\":\"Hongkiat.com\",\"url\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/wp-content\\\/uploads\\\/hkdc-logo-rect-yoast.jpg\",\"contentUrl\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/wp-content\\\/uploads\\\/hkdc-logo-rect-yoast.jpg\",\"width\":1200,\"height\":799,\"caption\":\"Hongkiat.com\"},\"image\":{\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/www.facebook.com\\\/hongkiatcom\",\"https:\\\/\\\/x.com\\\/hongkiat\",\"https:\\\/\\\/www.pinterest.com\\\/hongkiat\\\/\"]},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/#\\\/schema\\\/person\\\/e981676afae36d1ff5feb75094950ab3\",\"name\":\"Preethi Ranjit\",\"description\":\"A .NET developer with a JavaScript background, Preethi is expert in front-end coding, JavaScript, HTML, and CSS.\",\"url\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/author\\\/preethi\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"How to Use the MutationObserver API for Tracking DOM Changes - Hongkiat","description":"Imagine this scenario: Rita, a magazine writer, is editing an article online. She saves her changes and sees the message \"Changes saved!\" Just then, she","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.hongkiat.com\/blog\/mutationobserver-api\/","og_locale":"en_US","og_type":"article","og_title":"How to Use the MutationObserver API for Tracking DOM Changes","og_description":"Imagine this scenario: Rita, a magazine writer, is editing an article online. She saves her changes and sees the message \"Changes saved!\" Just then, she","og_url":"https:\/\/www.hongkiat.com\/blog\/mutationobserver-api\/","og_site_name":"Hongkiat","article_publisher":"https:\/\/www.facebook.com\/hongkiatcom","article_published_time":"2015-07-02T13:01:52+00:00","article_modified_time":"2024-07-30T11:21:16+00:00","og_image":[{"url":"https:\/\/assets.hongkiat.com\/uploads\/mutationobserver-api\/can-i-use.jpg","type":"","width":"","height":""}],"author":"Preethi Ranjit","twitter_card":"summary_large_image","twitter_creator":"@hongkiat","twitter_site":"@hongkiat","twitter_misc":{"Written by":"Preethi Ranjit","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.hongkiat.com\/blog\/mutationobserver-api\/#article","isPartOf":{"@id":"https:\/\/www.hongkiat.com\/blog\/mutationobserver-api\/"},"author":{"name":"Preethi Ranjit","@id":"https:\/\/www.hongkiat.com\/blog\/#\/schema\/person\/e981676afae36d1ff5feb75094950ab3"},"headline":"How to Use the MutationObserver API for Tracking DOM Changes","datePublished":"2015-07-02T13:01:52+00:00","dateModified":"2024-07-30T11:21:16+00:00","mainEntityOfPage":{"@id":"https:\/\/www.hongkiat.com\/blog\/mutationobserver-api\/"},"wordCount":760,"commentCount":5,"publisher":{"@id":"https:\/\/www.hongkiat.com\/blog\/#organization"},"image":{"@id":"https:\/\/www.hongkiat.com\/blog\/mutationobserver-api\/#primaryimage"},"thumbnailUrl":"https:\/\/assets.hongkiat.com\/uploads\/mutationobserver-api\/can-i-use.jpg","keywords":["DOM","NodeJS"],"articleSection":["Coding"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.hongkiat.com\/blog\/mutationobserver-api\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.hongkiat.com\/blog\/mutationobserver-api\/","url":"https:\/\/www.hongkiat.com\/blog\/mutationobserver-api\/","name":"How to Use the MutationObserver API for Tracking DOM Changes - Hongkiat","isPartOf":{"@id":"https:\/\/www.hongkiat.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.hongkiat.com\/blog\/mutationobserver-api\/#primaryimage"},"image":{"@id":"https:\/\/www.hongkiat.com\/blog\/mutationobserver-api\/#primaryimage"},"thumbnailUrl":"https:\/\/assets.hongkiat.com\/uploads\/mutationobserver-api\/can-i-use.jpg","datePublished":"2015-07-02T13:01:52+00:00","dateModified":"2024-07-30T11:21:16+00:00","description":"Imagine this scenario: Rita, a magazine writer, is editing an article online. She saves her changes and sees the message \"Changes saved!\" Just then, she","breadcrumb":{"@id":"https:\/\/www.hongkiat.com\/blog\/mutationobserver-api\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.hongkiat.com\/blog\/mutationobserver-api\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.hongkiat.com\/blog\/mutationobserver-api\/#primaryimage","url":"https:\/\/assets.hongkiat.com\/uploads\/mutationobserver-api\/can-i-use.jpg","contentUrl":"https:\/\/assets.hongkiat.com\/uploads\/mutationobserver-api\/can-i-use.jpg"},{"@type":"BreadcrumbList","@id":"https:\/\/www.hongkiat.com\/blog\/mutationobserver-api\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.hongkiat.com\/blog\/"},{"@type":"ListItem","position":2,"name":"How to Use the MutationObserver API for Tracking DOM Changes"}]},{"@type":"WebSite","@id":"https:\/\/www.hongkiat.com\/blog\/#website","url":"https:\/\/www.hongkiat.com\/blog\/","name":"Hongkiat","description":"Tech and Design Tips","publisher":{"@id":"https:\/\/www.hongkiat.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.hongkiat.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.hongkiat.com\/blog\/#organization","name":"Hongkiat.com","url":"https:\/\/www.hongkiat.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.hongkiat.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/www.hongkiat.com\/blog\/wp-content\/uploads\/hkdc-logo-rect-yoast.jpg","contentUrl":"https:\/\/www.hongkiat.com\/blog\/wp-content\/uploads\/hkdc-logo-rect-yoast.jpg","width":1200,"height":799,"caption":"Hongkiat.com"},"image":{"@id":"https:\/\/www.hongkiat.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/hongkiatcom","https:\/\/x.com\/hongkiat","https:\/\/www.pinterest.com\/hongkiat\/"]},{"@type":"Person","@id":"https:\/\/www.hongkiat.com\/blog\/#\/schema\/person\/e981676afae36d1ff5feb75094950ab3","name":"Preethi Ranjit","description":"A .NET developer with a JavaScript background, Preethi is expert in front-end coding, JavaScript, HTML, and CSS.","url":"https:\/\/www.hongkiat.com\/blog\/author\/preethi\/"}]}},"jetpack_featured_media_url":"https:\/\/","jetpack_shortlink":"https:\/\/wp.me\/p4uxU-6iN","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/posts\/24229","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/users\/145"}],"replies":[{"embeddable":true,"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/comments?post=24229"}],"version-history":[{"count":3,"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/posts\/24229\/revisions"}],"predecessor-version":[{"id":72435,"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/posts\/24229\/revisions\/72435"}],"wp:attachment":[{"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/media?parent=24229"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/categories?post=24229"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/tags?post=24229"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/topic?post=24229"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}