jQuery: The Write Less, Do More JavaScript Library

Ticket #205 (closed enhancement: wontfix)

Opened 2 years ago

Last modified 1 year ago

[PATCH] $().hasClass()

Reported by: brandon.aaron@… Assigned to: anonymous
Type: enhancement Priority: trivial
Milestone: Component: core
Version: Keywords:
Cc: Needs:

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

Change History

Changed 2 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.

Changed 2 years ago by brandon.aaron@…

  • status changed from new to closed
  • resolution set to wontfix

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.