I have a list of products that, when clicked, show a built-in pop-up that contains more information about the item with a click.
Now the fact is that this list also reacts, so on the desktop this list will consist of 4 columns, tables of the 3rd column, mobile 2 columns, etc.
I know that I can use jquery to count these list items and determine after which nth item this block should be displayed, but how would I achieve this when resizing the browser?
I set the example in jsfiddle .
$(".products li").click(function(e) { e.preventDefault(); $(".products li").removeClass("active"); $(this).addClass("active"); $(".popup-holder").after( $(".product-popup") ); var i = $(this).index(); var r = 4; do { if ( i > r ) { r += 4; if ( i == r) { r += 4; } } else { i++; } } while ( i != r ); $(".products li:nth-child("+i+")").after( $(".product-popup") ); $(".product-popup").show(); $(".product-popup span").text(i); });
Under a visual example of what I'm trying to achieve:

After clicking the list item, a pop-up window is displayed after the 4th item:

After resizing the browser, a pop-up should appear after the third element:

source share