tots tots - 23 days ago 11
HTML Question

How to get the value of the parent li in a nested list?

Can you help me, I create a nested list like this:

<ol>
<li data-value="1">
<ol>
<li data-value="1"></li>
<li data-value="2"></li>
<li data-value="3"></li>
</ol>
</li>
<li data-value="3"></li>
<li data-value="3"></li>
</ol>


How can I get the data-value attribute for the parent only?

sample output:
1,3,3

Answer

$('li').not('li ol li').each(function(){
console.log($(this).attr('data-value'))

})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ol>
   <li data-value="1">
        <ol>
           <li data-value="1"></li>
           <li data-value="2"></li>
           <li data-value="3"></li>
        </ol>
   </li>
   <li data-value="3"></li>
   <li data-value="3"></li>
</ol>

Try this one