.resize()

Bind an event handler to the "resize" JavaScript event, or trigger that event on an element.

.resize(function(eventObjectEvent))🡢 jQuery

function(eventObjectEvent) FunctionA function to execute each time the event is triggered.

.resize(eventData, function(eventObjectEvent))🡢 jQuery

eventData AnythingAn object containing data that will be passed to the event handler.
function(eventObjectEvent) FunctionA function to execute each time the event is triggered.

.resize()🡢 jQuery

This method is a shortcut for .on('resize', handler) in the first and second variations, and .trigger( "resize" ) in the third.

The resize event is sent to the window element when the size of the browser window changes:

$(window).resize(function () {
  $("#log").append("<div>Handler for .resize() called.</div>");
});

Now whenever the browser window's size is changed, the message is appended to <div id="log"> one or more times, depending on the browser.

Code in a resize handler should never rely on the number of times the handler is called. Depending on implementation, resize events can be sent continuously as the resizing is in progress (the typical behavior in Internet Explorer and WebKit-based browsers such as Safari and Chrome), or only once at the end of the resize operation (the typical behavior in some other browsers such as Opera).

To see the window width while (or after) it is resized, try:

HTML
$(window).resize(function () {
  $("body").prepend("<div>" + $(window).width() + "</div>");
});
DEMO

Looking for a Web Developer?

👋

Hi! I'm Basti, author of this site. If you are looking for a web developer with 15+ years of experience, holla at me!

Be it the good 'ol jQuery, vanilla JS or modern frameworks like Vue and Svelte, front- or backend, I can help you.

Just write me at jobs@jqapi.com :)