PiaklA
PiaklA

Reputation: 505

Collect class names under a <div> tag in java script array

<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/

Upvotes: 0

Views: 287

Answers (5)

gaurang171
gaurang171

Reputation: 9080

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

Upvotes: 0

nathan gonzalez
nathan gonzalez

Reputation: 11987

(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.

Upvotes: 0

Andreas
Andreas

Reputation: 21881

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.

Upvotes: 4

Prem
Prem

Reputation: 5964

var classNames = []  

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

Hope this is what you are looking for.

Upvotes: 0

tsukimi
tsukimi

Reputation: 1645

  $(document).ready(function () {
        $.each($("#input div"), function() {
           alert($(this).prop('class'));
        });

 });    

This fiddle

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

Upvotes: 0

Related Questions