javascript - Add specific class to child from parent -


i got list items child element. want each child of li class based on parents id class. elements getting id of first li.

html

<ul>   <li class="test post-91 other">     <a class='link'></a>    </li>   <li class="test post-43 other">     <a class='link'></a>   </li>   </ul> 

jquery

var id = $('.link').closest('li').attr("class").split(' ')[1]; id = id.replace(/[^0-9]/g, ''); id = "class-"+id; $('.link').addclass(id); 

i want elements number of "post-" parent li.

https://jsfiddle.net/f5lqzzew/

one more solution : here li can taken in loop , find anchor inside , assign class it.

$("ul li").each(function(){     var id = $(this).attr('class').split(' ')[1].replace(/[^0-9]/g, '');     $(this).find("a.link").text('class-' + id).addclass("class-"+id); // demo purposes }); 

updated fiddle


Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

Rendering JButton to get the JCheckBox behavior in a JTable by using images does not update my table -