Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8073539
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T14:22:04+00:00 2026-06-05T14:22:04+00:00

An element has a JavaScript style object which contains the different names and values

  • 0

An element has a JavaScript style object which contains the different names and values of CSS styles. I’d like to trigger a function every time this object changes without use of polling. Is there any way to do this in a way that is cross-browser compatible and would work reliably with third party code (because let’s say you’re providing a drop-in script)? Binding a JavaScript event like DOMAttrModified or DOMSubtreeModified won’t suffice because they don’t work in Chrome.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T14:22:05+00:00Added an answer on June 5, 2026 at 2:22 pm

    Edit 4: Live Demo

    $(function() {
      $('#toggleColor').on('click', function() {
        $(this).toggleClass('darkblue');
      }).attrchange({
        trackValues: true,
        callback: function(event) {
          $(this).html("<ul><li><span>Attribute Name: </span>" + event.attributeName + "</li><li><span>Old Value: </span>" + event.oldValue + "</li><li><span>New Value: </span>" + event.newValue + "</li></ul>");
        }
      });
    });
    body {
      font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
      font-size: 12px;
    }
    #toggleColor {
      height: 70px;
      width: 300px;
      padding: 5px;
      border: 1px solid #c2c2c2;
      background-color: #DBEAF9;
    }
    #toggleColor span {
      font-weight: bold;
    }
    #toggleColor.darkblue {
      background-color: #1A9ADA;
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    <script src="http://meetselva.github.io/attrchange/javascripts/attrchange.js"></script>
    <p>Click below div to toggle class darkblue.</p>
    <div id="toggleColor"></div>

    Edit 3: I have put all this together as a plugin that can be downloaded from git attrchange and here is the demo page.

    Edit 2:

    1. Fix for propertName in IE7 & IE8

    Edit 1:

    1. Handle multiple elements
    2. Ordered the conditions as MutationObserver, DOMAttrModified and onpropertychange for better implementation.
    3. Added modified Attribute Name to the callback.

    Thanks to @benvie for his feedback.

    DEMO: http://jsfiddle.net/zFVyv/10/ (Tested in FF 12, Chrome 19 and IE 7.)

    $(function() {
        (function($) {
            var MutationObserver = window.MutationObserver || window.WebKitMutationObserver || window.MozMutationObserver;
    
            function isDOMAttrModifiedSupported() {
                var p = document.createElement('p');
                var flag = false;
    
                if (p.addEventListener) p.addEventListener('DOMAttrModified', function() {
                    flag = true
                }, false);
                else if (p.attachEvent) p.attachEvent('onDOMAttrModified', function() {
                    flag = true
                });
                else return false;
    
                p.setAttribute('id', 'target');
    
                return flag;
            }
    
            $.fn.attrchange = function(callback) {
                if (MutationObserver) {
                    var options = {
                        subtree: false,
                        attributes: true
                    };
    
                    var observer = new MutationObserver(function(mutations) {
                        mutations.forEach(function(e) {
                            callback.call(e.target, e.attributeName);
                        });
                    });
    
                    return this.each(function() {
                        observer.observe(this, options);
                    });
    
                } else if (isDOMAttrModifiedSupported()) {
                    return this.on('DOMAttrModified', function(e) {
                        callback.call(this, e.attrName);
                    });
                } else if ('onpropertychange' in document.body) {
                    return this.on('propertychange', function(e) {
                        callback.call(this, window.event.propertyName);
                    });
                }
            }
        })(jQuery);
    
        $('.test').attrchange(function(attrName) {
            alert('Attribute: ' + attrName + ' modified ');
        }).css('height', 100);
    
    });
    

    Ref:

    1. Detect if DOMAttrModified supported
    2. DOMAttrModified for chrome
    3. Mutation Observer
    4. Why should we avoid using Mutation events?
    5. onPropertyChange IE

    Mutation Observers is the proposed replacement for mutation events in DOM4. They are expected to be included in Firefox 14 and Chrome 18

    Browser Support:

    onpropertychange – is supported in IE (tested in IE 7)

    DOMAttrModified – is supported in IE 9, FF and Opera

    MutationObservers – is very new and it worked fine in Chrome 18. Not sure how far it is supported and yet to be tested in Safari.

    Thanks @benvie on adding info about WebkitMutationObserver

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In Javascript/JQuery I have a need to copy one elements CSS style object to
I have a page, it has a drop down list, which contains the names
Has anybody bench marked selecting elements with id's and class's from CSS and javascript?
I'm interested in a way to check whether an element has display:none style explicility
How can I use jQuery to determine whether an element has a certain style
How can I make a function run after a certain element has finished animating?
On my page I am changing some css styles via javascript. When I try
I have a Javascript object that (very simplified) looks like this if('undefined' !== typeof(listCtrls)){
If a JavaScript function in a web page adds STYLE or SCRIPT elements to
The problem If the element has multiple classes then it will not match with

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.