Ask a question

Get Class List for Element with jQuery

Thanks for this – I was having a similar issue, as I’m trying to programatically relate objects will hierarchical class names, even though those names might not necessarily be known to my script.

In my script, I want an a tag to turn help text on/off by giving the a tag [some_class] plus the class of toggle, and then giving it’s help text the class of [some_class]_toggle. This code is successfully finding the related elements using jQuery:

$("a.toggle").toggle(function(){toggleHelp($(this), false);}, function(){toggleHelp($(this), true);});

function toggleHelp(obj, mode){
    var classList = obj.attr('class').split(/s+/);
    $.each( classList, function(index, item){
    if (item.indexOf("_toggle")  0) {
       var targetClass = "." + item.replace("_toggle", "");
       if(mode===false){$(targetClass).removeClass("off");}
       else{$(targetClass).addClass("off");}
    }
    });
} 

Article source: http://stackoverflow.com/questions/1227286/get-class-list-for-element-with-jquery

Comments are closed.