PortalP PortalP - 1 month ago 17
Javascript Question

How to activate .insertBefore on screen width change?

I have this snippet which switches the position of the secondary wrapper above the primary wrapper if the screen width is below 767px. This works great however it only works on refresh. How do I get it to work automatically when the screen width is changed?

Thanks! Total novice here.



jQuery('body.single #secondary').insertBefore('#primary');



It seems like you are using jquery so I would recommend the .resize() method.


This will fire whenever the window is resized. You can have it do whatever you need at that point.

    If (myWindow.width() >= windowThreshold){
        //do all the things...