jquery - Append element to :eq(2) if there are 3 or more on the page, or :eq(1) if there are less than 3 -
i'm working on page has variable number of elements class .def-set
. on pages have many 60 of them, , on others, have few 1.
i have been able appended element, .editorial-content-feed-replacement
, after 3rd instance of .def-set
class, want, i'm wondering if there way append after first or second instance of .def-set
if there less 3 on page.
my current code: $('.def-set:eq(2)').append('.editorial-content-feed-replacement')
you can calculate number of .def-set
elements with
let num = $(".def-set").length
and compare if-else logic accomplish want:
if (num >= 3) { $('.def-set:eq(2)').append('.editorial-content-feed-replacement'); } else { // append after last .def-set element $('.def-set').append('.editorial-content-feed-replacement'); }
Comments
Post a Comment