Best way to target nth-child elements to be added

I find the following methods somewhat monotonous when adding a new video. Basically, I am targeting every nth child element added.

$(document).ready(function(){
    $("li:nth-child(5) img").appendTo("li:nth-child(5) .video-gradient");
    $("li:nth-child(6) img").appendTo("li:nth-child(6) .video-gradient");
    $("li:nth-child(7) img").appendTo("li:nth-child(7) .video-gradient");
    $("li:nth-child(8) img").appendTo("li:nth-child(8) .video-gradient");
    $("li:nth-child(9) img").appendTo("li:nth-child(9) .video-gradient");
}

is there a better way to implement the code (allows ay in a for loop) that will prevent me from adding an nth-child element to the added video element?

Thanks a lot Robbie

+4
source share
1 answer

How about a for loop ?

for (var i = 5; i < 10; i++) {
     $("li:nth-child(" + i + ") img").appendTo("li:nth-child(" + i + ") .video-gradient");
}
+3
source

Source: https://habr.com/ru/post/1685741/


All Articles