PortalP PortalP - 1 year ago 107
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');


Answer Source

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...
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download