0

What I am trying to achieve is just to get all class names which are used with a certain element.

The problem is that when I use my various methods to get the class names, only the first class is being returned.

Lets say I have a table, where each <td> tags have the classes classOne classPart1 classThree

and will thus look something like this:

<table id='gridMain'>
    <tr>
        <td class = 'classOne classPart1 classThree'></td>
        <td class = 'classOne classPart2 classThree'></td>
        <td class = 'classOne classPart3 classThree'></td>
        <td class = 'classOne classPart4 classThree'></td>
        <td class = 'classOne classPart5 classThree'></td>
    </tr>
</table>

What I've tried is the following:

var classArray = [];
$("#gridMain .classOne").each(function (ix, element) 
{
    //Example 1 
    alert($(this).attr('class'));

    //Example 2
    $($(this).attr('class').split(' ')).each(function() 
    { 
        classArray.push(this);
    });

    //Example 3
    alert(this.className);
}

In Example 1 only 'classOne is returned'. In Example 2, I try to split the classes and create an array, with the same result. The Array Length is 1. Only 'classOne is returned'. And in example 3, only classOne is returned...

Anyone know why this is happening?

Thank you in advance!

Phillip-juan
  • 546
  • 4
  • 29
  • 1
    looks fine here http://jsfiddle.net/bipen/Stmmw/.. your were missing `)` end bracketin the question which i think is not the case... – bipen Jun 21 '13 at 11:13
  • It does work yes, which is strange, because it's not working in my script locally. – Phillip-juan Jun 21 '13 at 11:17
  • make sure.. none of the other script is breaking your code.. it will be hard to help you without seeing your other code – bipen Jun 21 '13 at 11:19
  • I've found the problem. The order in which I instantiated my code was wrong. By the time I tried to retrieve the classes, they have not yet been instantiated. A mistake on my part... – Phillip-juan Jun 21 '13 at 12:17

0 Answers0