Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

jquery above and below screen sizes

I have added a script for showing a div before different divs in different screen size. This is the code I used:

jQuery(function($){ 
jQuery(document).ready(function(){
    jQuery(window).on('resize', function(){
        if(jQuery(window).width() <= 1024){
            jQuery( ".checkout.woocommerce-checkout .woocommerce-shipping-fields__wrapper" ).insertBefore( ".checkout.woocommerce-checkout .flux-step.flux-step--2 .flux-checkout__shipping-table" );
        }
        else if(jQuery(window).width() >= 1025){
            jQuery( ".checkout.woocommerce-checkout .woocommerce-shipping-fields__wrapper" ).insertBefore( ".checkout.woocommerce-checkout .flux-checkout__content-right #order_review" );
        }
    }); 
}); 
});

But the code is not working when I open the site. It only works if I resize the screen. May be due to the resize function is used.

Can anyone please guide me how to make it so that it’ll show the 2 conditions even without resizing the screen and one’ll work above 1024px and another below 1024px.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

TIA

>Solution :

Just put your code in a function and call it on the document ready:

$(function(){
  
  resize();
  
  $(window).on('resize', resize);

  function resize(){
    $( ".checkout.woocommerce-checkout .woocommerce-shipping-fields__wrapper" )
      .insertBefore(
        $(window).width() <= 1024 ? 
        ".checkout.woocommerce-checkout .flux-step.flux-step--2 .flux-checkout__shipping-table" : 
        ".checkout.woocommerce-checkout .flux-checkout__content-right #order_review"
      );
   }
   
}); 
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading