I want to get class names of children at several levels. I am trying to follow, but it only gives the class names of the back child elements. What am I doing wrong?
<script type="text/javascript">
$(document).ready(function(){
thisP=$("#myParagraph");
getChildStyles(thisP);
function getChildStyles(thisobj) {
var classNames;
var classNames1;
$(thisobj).children().each(function(){
classNames+=$(this).attr('class');
if($(this).children().length>0) {
classNames1+=getChildStyles($(this));
}
classNames+=classNames1;
});
return classNames;
}
});
</script>
And HTML,
<ul id="myParagraph" class"mainUL">
<li id="LIOne">ksjdfhsdf</li>
<li id="LITwo">skdjfkdsf<span class"span1Class"><span class="span2class"></span>
</span></li>
<li id="LIThree" class="thirdLIClass">edroiutret</li>
</ul>
source
share