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.
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 });
Comments
Post a Comment