JQuery Iterating around DISTINCT classes in a page

I am looking to iterate over each class defined in my html page once. I can get cool names with JQuery, but I'm looking for a way to just iterate over each individual class name once, not every time it appears on the page.

0


source to share


1 answer


Scant to do it, but here you go



<html>
<head>
   <title>Page</title>  
   <script src="jquery-1.3.2.min.js"></script>
   <script>
       $(function() {

           var classes = {};
           $("*[class]").each(function() {
               var cs = $(this).attr("class").split(/\s/g);
               for (var i = 0; i < cs.length; i++) {
                   if (!classes[cs[i]]) {
                       classes[cs[i]] = true;
                   }
               }
           });

           for (var prop in classes)
               alert(prop);
       });
</script>
</head>
   <body>

   <div class="single"></div>
   <div class="one two three"></div>
   <div class="one two three"></div>
   <div class="one two three four"></div>
   <div class="one two three five"></div>
   <div class="one two three six"></div>

   </body>
</html>

      

+1


source







All Articles