PWToggleAttr - A jQuery Attribute Toggle Plugin


/ Published in: jQuery
Save to your folder(s)

## PWToggleAttr

This jQuery Plugin allows you to toggle an attribute on/off on an element. e.g. the disabled element of an input. Examples and such are available on http://www.pixaweb.co.uk/resources


Copy this code and paste it in your HTML
  1. /*
  2.  * @package pwToggleAttr
  3.  * @description toggles an attribute of an element on click
  4.  * @author Jared Clarke <[email protected]>
  5.  * @copyright 2010 (c) Jared Clarke @ pixaweb.co.uk
  6.  * @version 0.1
  7.  */
  8. (function ($) {
  9.  
  10. $.fn.pwToggleAttr = function (options) {
  11.  
  12. var opts = $.extend({}, $.fn.pwToggleAttr.defaults, options);
  13.  
  14. return this.each(function () {
  15.  
  16. var _this = $(this),
  17. _target = (opts.target == false) ? _this : $(opts.target);
  18.  
  19. _this.click( function (event) {
  20.  
  21. event.preventDefault();
  22.  
  23. _target.attr(opts.attr, !_target.attr(opts.attr));
  24.  
  25. });
  26.  
  27. return _this;
  28.  
  29. });
  30.  
  31. };
  32.  
  33. $.fn.pwToggleAttr.defaults = {
  34. target : false,
  35. attr : "disabled"
  36. };
  37.  
  38. })(jQuery);

URL: http://www.pixaweb.co.uk/resources?tag=jquery

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.