0
<script type="text/javascript">
 $(document).ready(function () {

 });

</script>


<div id="input">
    <div class="feature draggable">Drag 1</div>
    <div class="feature resizable">Resize</div>
    <div class="feature downloadable">Download</div>
    <div class="feature draggable">Drag 2</div>
    <div class="feature trackable">Track</div>
    <div class="feature colorable">Color</div>
</div>​

I want to store all the class elements under the <div id="input"> in array. What is the correct way to do it?

JS Fiddle: http://jsfiddle.net/gMjxu/

nhahtdh
  • 55,989
  • 15
  • 126
  • 162
PiaklA
  • 495
  • 2
  • 7
  • 21
  • See the answer here: http://stackoverflow.com/questions/1227286/get-class-list-for-element-with-jquery – Grzegorz Aug 30 '12 at 04:33

5 Answers5

4
var classnames = $("#input div").map(function() { return this.className }).get();

If there will be more than one class on an element you will have to do some extra work.

Andreas
  • 21,535
  • 7
  • 47
  • 56
0
  $(document).ready(function () {
        $.each($("#input div"), function() {
           alert($(this).prop('class'));
        });

 });    

This fiddle

http://jsfiddle.net/dappledore/gMjxu/17/

tsukimi
  • 1,605
  • 2
  • 21
  • 36
  • This will give me everything under the
    tag , I am trying to find a way if I could retrieve only class names under the
    tag
    – PiaklA Aug 30 '12 at 04:34
0
(function($) {
    $.fn.classes = function(f) {
        var c = [];
        $.each(this, function(i, v) {
            var _ = v.className.split(/\s+/);
            for (var j in _)'' === _[j] || c.push(_[j]);
        });
        c = $.unique(c);
        if ("function" === typeof f) for (var j in c) f(c[j]);
        return c;
    };
})(jQuery);

$(document).ready(function() {
    alert($('#input div').classes());
});​

$(document).ready(function () {
alert($('#input div').classes());
    });
</script>


<div id="input">
        <div class="feature draggable">Drag 1</div>
        <div class="feature resizable">Resize</div>
        <div class="feature downloadable">Download</div>
        <div class="feature draggable">Drag 2</div>
        <div class="feature trackable">Track</div>
        <div class="feature colorable">Color</div>
    </div>​

should do the trick. http://jsfiddle.net/FRhKS/

you end up with an array of unique classes for all selected elements.

nathan gonzalez
  • 11,817
  • 4
  • 41
  • 57
  • 1
    The fiddle has the same script as the one from the TO. Additionally you shouldn't use `for(... in...)` for arrays – Andreas Aug 30 '12 at 04:48
  • @Andreas, i see no problem with it, though i will admit to having posted the wrong link. – nathan gonzalez Aug 30 '12 at 04:52
  • `for..in` on Array objects is a bad plan unless you're 100% sure your code will never be used with somebody else's code (including third-party libraries). Even if you never add non-numeric properties to an array other people might and it could break your code. (Especially if you use a library that adds methods to `Array.prototype`.) – nnnnnn Aug 30 '12 at 05:30
0
var classNames = []  

$('#input div').each(function(){
     classNames.push($(this).attr('class')) });

Hope this is what you are looking for.

chhameed
  • 4,406
  • 4
  • 26
  • 44
Prem
  • 5,844
  • 3
  • 25
  • 23
0

Here i have complete demo bins and link as below:

Demo http://codebins.com/bin/4ldqp7w

HTML

<div id="input">
  <div class="feature draggable">
    Drag 1
  </div>
  <div class="feature resizable">
    Resize
  </div>
  <div class="feature downloadable">
    Download
  </div>
  <div class="feature draggable">
    Drag 2
  </div>
  <div class="feature trackable">
    Track
  </div>
  <div class="feature colorable">
    Color
  </div>
</div>
<input type="button" value="Show Classes" id="btn1" />

jQuery

$(function() {
    var c = [];
    $("#input div").each(function() {
        var cls = $(this).attr('class').split(' ');
        for (var j = 0; j < cls.length; j++) {
            if (cls[j] != '') {
                c.push(cls[j]);
            }
        }
    });

    $("#btn1").click(function() {
        if (c.length <= 0) {
            alert("No Class Exists...!");
        } else {
            for (var i = 0; i < c.length; i++) {
                alert(c[i]);
            }
        }
    });

});

Demo http://codebins.com/bin/4ldqp7w

gaurang171
  • 9,032
  • 4
  • 28
  • 30