I try to cible juste the next div with indented
class name. in the front i have all the next div with indented
having the same id.
actually, each div with indented
as a class name, should have the id of the <li>
that is right before it. This is my js:
if ($('.o-commentaires-items').length) {
var cls = $('.o-commentaires-items').find($(".comment-parent")).attr('id').split(' ')[0];
number = cls.substr(cls.lastIndexOf("-") + 1);
console.log("closest : ", 'collapsee-' + number);
$('.o-commentaires-items').find($(".comment-parent").next(".indented").addClass("collapse").attr({
"id": "collapse-"+number,
"aria-labelledby": "heading-"+number
}));
}
Here the html that the inspector shows in chrome:
<ul class="o-commentaires-items accordion">
<li data-comment-user-id="1" id="comment-137" class="js-ajax-comments-id-137 contextual-region js-comment o-commentaires-item comment-parent by-viewer" data-comment-parent="0">
test
</li>
<div class="indented collapse" id="collapse-137" aria-labelledby="heading-137" style="">
<li data-comment-user-id="1" id="comment-139" class="js-ajax-comments-id-139 contextual-region js-comment o-commentaires-item comment-child comment-parent-137 by-viewer" data-comment-parent="137">
child 1
</li>
<li data-comment-user-id="1" id="comment-140" class="js-ajax-comments-id-140 contextual-region js-comment o-commentaires-item comment-child comment-parent-137 by-viewer" data-comment-parent="137">
child 2
</li>
</div>
<li data-comment-user-id="1" id="comment-136" class="js-ajax-comments-id-136 contextual-region js-comment o-commentaires-item comment-parent by-viewer" data-comment-parent="0">
test 2
</li>
<div class="indented collapse" id="collapse-137" aria-labelledby="heading-137" style="">
<li data-comment-user-id="1" id="comment-138" class="js-ajax-comments-id-138 contextual-region js-comment o-commentaires-item comment-child comment-parent-136 by-viewer" data-comment-parent="136">
child 1
</li>
</div>
</ul>
You can use .each
loop to iterate through all indented
div then using :first
get first li inside that div. Then, simply add attributes to your elements ie: .attr()
.
Demo Code :
if ($('.o-commentaires-items').length) { //loop $(".indented").each(function() { console.log($(this).find("li:first").attr("id")) //get first li attr id var id = $(this).find("li:first").attr("id").split('-')[1] //add attrs.. $(this).addClass("collapse").attr({ "id": "collapse-" + id, "aria-labelledby": "heading-" + id }); }) }
.collapse { border: 1px solid }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <ul class="o-commentaires-items accordion"> <li data-comment-user-id="1" id="comment-137" class="js-ajax-comments-id-137 contextual-region js-comment o-commentaires-item comment-parent by-viewer" data-comment-parent="0"> test </li> <div class="indented collapse" id="collapse-137" aria-labelledby="heading-137" style=""> <li data-comment-user-id="1" id="comment-139" class="js-ajax-comments-id-139 contextual-region js-comment o-commentaires-item comment-child comment-parent-137 by-viewer" data-comment-parent="137"> child 1 </li> <li data-comment-user-id="1" id="comment-140" class="js-ajax-comments-id-140 contextual-region js-comment o-commentaires-item comment-child comment-parent-137 by-viewer" data-comment-parent="137"> child 2 </li> </div> <li data-comment-user-id="1" id="comment-136" class="js-ajax-comments-id-136 contextual-region js-comment o-commentaires-item comment-parent by-viewer" data-comment-parent="0"> test 2 </li> <div class="indented collapse" id="collapse-137" aria-labelledby="heading-137" style=""> <li data-comment-user-id="1" id="comment-138" class="js-ajax-comments-id-138 contextual-region js-comment o-commentaires-item comment-child comment-parent-136 by-viewer" data-comment-parent="136"> child 1 </li> </div> </ul>
thanks to the code provided to me by @Swati i was able to fin a solution:
if ($('.o-commentaires-items').length) {
//loop
$(".indented").each(function() {
console.log($(this).prev(".comment-parent").attr("id"))
//get first li attr id
var id = $(this).prev(".comment-parent").attr("id").split('-')[1];
//add attrs..
$(this).addClass("collapse").attr({
"id": "collapse-" + id,
"aria-labelledby": "heading-" + id
});
})
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.