I have a bunch of Paragraphs in a Section. In desktop I want to wrap every 3 paragraphs in a row, in Tablets I want to wrap every 2 paragraphs in a row, and in mobiles no wrap should happen.
I have written the following code and it is working, but the problem is when I change the resolution or switch Desktop to Tablet mode then the condition is not getting applied automatically until I refresh the page.
(function ($, Drupal, drupalSettings) {
Drupal.behaviors.my_theme_name = {
attach: function (context, settings) {
if (($(window).outerWidth() >= 768) && ($(window).outerWidth() <= 1199)){
var divs = $("section > p");
for(var i = 0; i < divs.length; i+=2) {
divs.slice(i, i+2).wrapAll("<div class='row'></div>");
}
}
if (window.outerWidth >= 1200) {
var divs = $("section > p");
for(var i = 0; i < divs.length; i+=3) {
divs.slice(i, i+3).wrapAll("<div class='row'></div>");
}
}
}
};
})(jQuery, Drupal, drupalSettings)
Is there a way, apply jQuery condition automatically on screen size change without page refresh ??
Note: This JS file is using in Drupal site.