jQuery resize() Method

Last Updated:

jQuery resize() Method

The jQuery resize() method is used for the following 2 purpose

  1. To run a callback when a specified element is resized.
  2. To trigger the resize event of a specified element.

Run a Callback

In the following example, we will run a callback function when a window is resized.

Example

HTML Editor
<!DOCTYPE html> <html lang="en-US"> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script> </head> <body> <h1>jQuery resize() Method</h1> <div>Try to resize this window.</div> <p>Window width is <span></span></p> <script> $(document).ready(function(){ $(window).resize(function() { $("span").text($(window).width() +"px"); }); }); </script> </body> </html>

Syntax

// Trigger the resize event $(selector).resize() // Attach a callback to resize event $(selector).resize(callback)

Parameter Values

Value Type Explanation
callback Optional Specifies the function to run when the resize event is triggered.

Browser Support

Devicedesktopmobile
Browsergoogle chromesafarifirefoxinternet Exploreredgeoperagoogle chromesafarifirefoxedgeoperaandroid webviewsamsung internet
resize()YesYesYesYesYesYesYesYesYesYesYesYesYes

Trigger a resize Event

In the following example, we will trigger a resize event when a button is clicked.

Example

HTML Editor
<!DOCTYPE html> <html lang="en-US"> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script> </head> <body> <h1>jQuery resize() Method</h1> <div>Try to resize this window or click on the button.</div> <p>Window width is <span></span></p> <button>Trigger resize Event</button> <script> $(document).ready(function(){ $(window).resize(function() { $("span").text($(window).width() +"px"); }); $("button").click(function(){ $(window).resize(); }); }); </script> </body> </html>

Share this Page

Meet the Author