I am in a situation where I need to use .wrap and: first-child.
This is what I do:
<script>$("a").wrap("<div class='category-wrapper'></div>");</script> <script>$("div.category-wrapper:first-child").addClass("first");</script>
This should make the div.category wrapper out of the link, and then add the βfirstβ class to every first div.category-wrapper.
Conclusion:
<div class="category-wrapper"><a href="#">Test</a></div>
It's good! However, I cannot get the "first child" to work (he does not add the "first" class). If I use it somewhere else, it works, so I'm sure this is due to the dynamic rendering of the previous item.
Output Example:
<div class="category-wrapper"><a href="#">Test #1</a></div> <div class="category-wrapper"><a href="#">Test #2</a></div> <div class="category-wrapper"><a href="#">Test #3</a></div> <div class="category-wrapper"><a href="#">Test #4</a></div>
Required Conclusion:
<div class="category-wrapper first"><a href="#">Test #1</a></div> <div class="category-wrapper"><a href="#">Test #2</a></div> <div class="category-wrapper"><a href="#">Test #3</a></div> <div class="category-wrapper"><a href="#">Test #4</a></div>
However, I cannot get it to work.
source share