Bug Tracker

Opened 14 years ago

Closed 14 years ago

Last modified 13 years ago

#205 closed enhancement (wontfix)

[PATCH] $().hasClass()

Reported by: brandon.aaron@… Owned by:
Priority: trivial Milestone:
Component: core Version:
Keywords: Cc:
Blocked by: Blocking:

Description

I use hasClass a lot and wish it was in the core to keep my code more clean. Here is a patch that even includes the DOCS and a test for it:

Index: jquery/src/jquery/jquery.js
===================================================================
--- jquery/src/jquery/jquery.js (revision 312)
+++ jquery/src/jquery/jquery.js (working copy)
@@ -3175,6 +3175,27 @@
    removeClass: function(c){
      jQuery.className.remove(this,c);
    },
+   
+   /**
+    * Checks to see if the specified class exists already on 
+    * the matched element.
+    * 
+    * @example $("p").hasClass("selected")
+    * @before <p class="selected">Hello</p>
+    * @result [ true ]
+    *
+    * @test var div = $("div").addClass("test");
+    * var pass = div.hasClass("test");
+    * ok( pass, "Has Class");
+    *
+    * @name hasClass
+    * @type jQuery
+    * @param String class A CSS class to check for
+    * @cat DOM
+    */
+   hasClass: function(c){
+     return jQuery.className.has(this,c);
+   },
 
    /**
     * Adds the specified class if it is present, removes it if it is

Change History (2)

comment:1 Changed 14 years ago by joern

Is this really useful? Consider this:

$("p").is(".selected")

It's even shorter then hasClass(). I think we need at least an example that hasClass() is significantly faster then is(), before adding it.

comment:2 Changed 14 years ago by brandon.aaro

Resolution: wontfix
Status: newclosed

Ahhh ... Looky there ... A new feature of jQuery I didn't know existed. To think I almost hit up the mailing list before submiting this too. Going ahead and marking as closed. Thanks joern.

Note: See TracTickets for help on using tickets.