javascript - How to get only <li> tag's innnerHTML from the content having html code -


i new javascript. can provide idea how li tag innerhtml in array. example:

var content = '<li>item 1</li> <li>item 2  <ol>   <li>item 3</li>   <li>item 4</li>  </ol> </li> <li>item 5</li>' 

i want iterate such can output follows:

arr[0]='<li>item 1</li>' arr[1]='<li>item 2  <ol>   <li>item 3</li>   <li>item 4</li>  </ol> </li>' arr[2]='<li>item 5</li>' 

moreover, nested li can used iterating in nested ol.

i want use document.getelementsbytagname("li"); should use content variable data.

thanks.

try using jquery

var el = $('<div />').append( content  ),     ar  = [];  el.find('>li').each( function(){     ar.push(         $(this).clone().wrap('<p>').parent().html()     ) });  console.log( ar ); 

and print in console console.log( ar );

[  "<li>item 1</li>" ,   "<li>item 2<ol><li>item ...i>item 4</li></ol></li>","<li>item 5</li>" ,  "<li>item 5</li>" ] 

so can access ar[0] or 1 .. 2 etc ...


Comments