How to retrieve class names of all child elements at multiple level in jquery? -


i want retrieve class names of child elements @ multiple level. i'm trying following, gives class names of rear child elements. doing wrong ?

    <script type="text/javascript">     $(document).ready(function(){     thisp=$("#myparagraph");     getchildstyles(thisp);    //function  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> 

you can quick array of names bit simpler using .map(), this:

$(document).ready(function(){    var arrayofclassnames = $("#myparagraph").find("[class]").map(function() {      return this.classname;    }).get(); }); 

you can test out here.

if want string can arrayofclassnames.join('')...or want use really.


Comments

Popular posts from this blog

asp.net - repeatedly call AddImageUrl(url) to assemble pdf document -

java - Android recognize cell phone with keyboard or not? -

iphone - How would you achieve a LED Scrolling effect? -