Sha256: b985450e4de233bbbae41b7ac87ad195f335777f52f81ebcef706a6bd29c13bc

Contents?: true

Size: 1.14 KB

Versions: 8

Compression:

Stored size: 1.14 KB

Contents

(function($){
  "use strict";

  $.fn.changeTag = function(tagName, callback){

    return $(this.map(function(){
      var $newTag = $('<' + tagName + '/>');

      //copy all attributes
      $.each(this.attributes, function(_, attribute) {
        $newTag.attr(attribute.name, attribute.value);
      });

      //copy contents
      $newTag.append($(this).contents());

      //call callback passing the old element
      if (callback) {
        var newTag = callback.call($newTag, this);
        if (newTag instanceof jQuery) {
          $newTag = newTag;
        }
      }

      //clone events
      var events = $._data(this, "events");
      $.each(events, function(_, events) {
        $.each(events, function(_, event) {
          //Name spaces
          var eventType = event.type;
          if (event.namespace.length > 0) {
            eventType += '.' + event.namespace
          }
          $newTag.bind(eventType, event.data, event.handler);
        });
      });

      //replace old tag
      $(this).replaceWith($newTag);

      //return element to can create a jQuery object from new elements
      return $newTag[0];

    }));


  };

})(jQuery);

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
cd2_catton_cms-1.1.35 app/assets/javascripts/c/change_tag.js
cd2_catton_cms-1.1.34 app/assets/javascripts/c/change_tag.js
cd2_catton_cms-1.1.33 app/assets/javascripts/c/change_tag.js
cd2_catton_cms-1.1.32 app/assets/javascripts/c/change_tag.js
cd2_catton_cms-1.1.31 app/assets/javascripts/c/change_tag.js
cd2_catton_cms-1.1.30 app/assets/javascripts/c/change_tag.js
cd2_catton_cms-1.1.20 app/assets/javascripts/c/change_tag.js
cd2_catton_cms-1.1.10 app/assets/javascripts/c/change_tag.js