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/

1

5 Answers 5

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks this helped me to understand , was not aware of .map
+1. But note that you'd probably want to throw a .get() on the end to return an actual array rather than a jQuery object.
for the more than one class on an element answer, please see my solution.
@nnnnnn Good point. I've adjusted my solution as suggested :-)
0
  $(document).ready(function () {
        $.each($("#input div"), function() {
           alert($(this).prop('class'));
        });

 });    

This fiddle

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

1 Comment

This will give me everything under the <div id> tag , I am trying to find a way if I could retrieve only class names under the <div id="input"> tag
0
var classNames = []  

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

Hope this is what you are looking for.

Comments

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.

3 Comments

The fiddle has the same script as the one from the TO. Additionally you shouldn't use for(... in...) for arrays
@Andreas, i see no problem with it, though i will admit to having posted the wrong link.
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.)
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.