Skip to content
Advertisement

Bind Ready Function and Resize Event

In jQuery, is it possible to somehow “bind” an action to both the ready function and a resize event? I’m trying to add/remove classes on resize and I want the initial class state to be there on ready.

Advertisement

Answer

The easiest way to do this is to trigger the resize event immediately after binding it:

$(document).ready(function() {
    $(window).resize(function() {
        // code to run on resize
    }).resize(); // trigger resize handlers
});
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement