]> source.dussan.org Git - jquery-ui.git/commitdiff
Core: Add the uniqueId() and removeUniqueId() methods written by @scottgonzalez to...
authorkborchers <kris.borchers@gmail.com>
Wed, 30 May 2012 01:55:43 +0000 (20:55 -0500)
committerkborchers <kris.borchers@gmail.com>
Wed, 30 May 2012 02:13:17 +0000 (21:13 -0500)
tests/unit/core/core.js
ui/jquery.ui.core.js

index 7a78ae34d455765840dcea4fbe4ef12b2ed6f3ef..1026c5b0b607d5690eb680c3ddfe7e2fdc95aace 100644 (file)
@@ -153,4 +153,14 @@ test( "outerHeight(true) - setter", function() {
        equal( el.height(), 32, "height set properly when hidden" );
 });
 
+test( "uniqueId / removeUniqueId", function() {
+       var el = $( "img" ).eq( 0 );
+
+       equal( el.attr( "id" ), undefined, "element has no initial id" );
+       el.uniqueId();
+       ok( /ui-id-\d+$/.test( el.attr( "id" ) ), "element has generated id" );
+       el.removeUniqueId();
+       equal( el.attr( "id" ), undefined, "unique id has been removed from element" );
+});
+
 })( jQuery );
index d8fff91e1da157b51429fb738365d764b379653a..a511de24bf59d4bbb994d3985e93283f1f507dd9 100644 (file)
@@ -9,6 +9,9 @@
  */
 (function( $, undefined ) {
 
+var uuid = 0,
+       runiqueId = /^ui-id-\d+$/;
+
 // prevent duplicate loading
 // this is only a problem because we proxy existing functions
 // and we don't want to double proxy them
@@ -107,6 +110,22 @@ $.fn.extend({
                return 0;
        },
 
+       uniqueId: function() {
+               return this.each(function() {
+                       if ( !this.id ) {
+                               this.id = "ui-id-" + (++uuid);
+                       }
+               });
+       },
+
+       removeUniqueId: function() {
+               return this.each(function() {
+                       if ( runiqueId.test( this.id ) ) {
+                               $( this ).removeAttr( "id" );
+                       }
+               });
+       },
+
        disableSelection: function() {
                return this.bind( ( $.support.selectstart ? "selectstart" : "mousedown" ) +
                        ".ui-disableSelection", function( event ) {