views:

31

answers:

2

Can you change classes without id when your classes are dynamic and only the parent has an id?

I have something like:

<div id="number_block">
  <div class="one science easy left"></div>
  <div class="one science easy center"></div>
  <div class="one science easy right"></div>
</div>

I only reach this part

var number_block_children = Dom.getChildren('number_block');
for(var i=0; i < number_block_children.length; i++)
{
     /* I don't know the syntax to change class name here for every child, is it possible?
      * I can't use Dom.getElementByClassName...since the class is dynamic.
      * It's something similar to how get classname by id, only I don't have id, just parent id:
      *        Dom.get('id-name-here').className
      * I can't figure out how to do this....
      */

}

Thanks!

A: 

hey you can do something like

YAHOO.util.Event.addListener(window, "load", function() {
    var number_block_children = YAHOO.util.Dom.getChildren('number_block');
    for(var i=0; i < number_block_children.length; i++)
    {
        console.log(YAHOO.util.Dom.hasClass(number_block_children[i], 'one'));
        console.log(YAHOO.util.Dom.hasClass(number_block_children[i], 'two'));
    }
});

hope it helps

jebberwocky
+1  A: 

You can use getAttribute to get element classes:

var number_block_children = YAHOO.util.Dom.getChildren('number_block');
for(var i=0; i < number_block_children.length; i++)
{

    var class = YAHOO.util.Dom.getAttribute(number_block_children[i], 'class');
    var classes = class.split(' ');

}
jira
Thanks! I don't know about YUI getAttribute :D Nice!
Woppi