0

Is there a trigger within jQuery that will detect changes in attributes' values?

As an example:

<div id="thisField" value-grabbed='1'></div>

to

<div id="thisField" value-grabbed='2'></div>

and jQuery gives me an alert after the change saying "done."

EDIT----

I'll more than likely going to change the attributes' value using the .attr() code.

Majo0od
  • 2,278
  • 10
  • 33
  • 58
  • 6
    Your code is incorrect, don't make use of some custom attributes, use `data` attribute instead: `data-grabbed='1'` – Roko C. Buljan May 02 '13 at 18:30
  • How are you changing that data? – tymeJV May 02 '13 at 18:31
  • @roXon I had no idea that was a legitimate attribute. How come that's different? I'm interested in knowing! – Majo0od May 02 '13 at 18:33
  • @roXon I'd say incorrect is too strong. The page will be invalid "in the eyes of" HTML5, but it shall render just fine. It is really better to use `data-` attributes, though. – acdcjunior May 02 '13 at 18:34
  • Just read about it here if anyone is curious: http://ejohn.org/blog/html-5-data-attributes/ – Majo0od May 02 '13 at 18:36
  • 1
    @acdcjunior yes, it's hard-worded, but we should all be evangelists of well-written-code, and use what technology and consortium gives us at disposition. That means It's not wrong to play with custom "attributes" **once the DOM is parsed** `8-D` – Roko C. Buljan May 02 '13 at 18:43
  • I totally agree with you @roXon. If it isn't well written, then I won't be happy. Thanks for bringing this to my attention. :-) – Majo0od May 02 '13 at 18:44

1 Answers1

4

Yes, however you have to create said trigger yourself.

$(element).attr("value-grabbed","2").trigger("attrchange");

You can automate it by overriding the attribute method:

(function($){ // avoid global conflicts
    var oldfn = $.fn.attr;

    $.fn.attr = function(){    
        var ret = oldfn.apply(this,arguments);
        if (arguments.length === 2 || typeof arguments[0] === "object") {
            this.trigger("attrchange");
        }            
        return ret;
    }
})(jQuery);
Kevin B
  • 94,570
  • 16
  • 163
  • 180