Search code examples
javascriptjqueryeventsbindjquery-events

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.


Solution

  • 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
    });