Skip to main content

Bug Tracker

Side navigation

#205 closed enhancement (wontfix)

Opened September 18, 2006 04:31AM UTC

Closed September 18, 2006 03:24PM UTC

Last modified June 21, 2007 02:27AM UTC

[PATCH] $().hasClass()

Reported by: brandon.aaron@gmail. 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

Attachments (0)
Change History (2)

Changed September 18, 2006 09:53AM UTC by joern comment:1

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.

Changed September 18, 2006 03:24PM UTC by brandon.aaro comment:2

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.