How to increase the size of a division when you click it using jQuery ?
Last Updated :
24 Apr, 2021
In this article, we will learn how to increase the size of a division when you click on it using jQuery.
Approach 1: Using the height() and width() method.
All the divisions on the page are first selected using a common selector and a click binding is applied to trigger the function to increase the size using the click() method. The division that is then currently clicked can then be found out by using this as the selector.
The current height of the element can be found out using the height() method and the current width can be found using the width() method. These are stored temporarily in separate variables. The same methods are then used to set the modified height and width. The new values can be calculated by multiplying the current height and width with a multiplier. This multiplier can be specified as a variable. This will increase the size of the clicked division equally.
We can also use different multipliers to increase the height and width of the divisions separately.
Syntax:
$(".box").click(function () {
// Select the clicked element
let curr_elem = $(this);
// Get the current dimensions
let curr_width = curr_elem.width();
let curr_height = curr_elem.height();
// Set the new dimensions
curr_elem.height(curr_height * increase_modifier);
curr_elem.width(curr_width * increase_modifier);
});
The below examples illustrate the above approach:
Example:
HTML
<html>
<head>
<script src=
"https://code.jquery.com/jquery-3.3.1.min.js">
</script>
<style>
.container {
display: flex;
}
.box {
height: 100px;
width: 100px;
margin: 10px;
}
.red-bg {
background-color: red;
}
.green-bg {
background-color: green;
}
.yellow-bg {
background-color: yellow;
}
</style>
</head>
<body>
<h1 style="color: green">
GeeksforGeeks
</h1>
<b>
How to increase the size of a
division when you click it using jQuery?
</b>
<p>Click on a div to increase its size</p>
<div class="container">
<div class="box red-bg"></div>
<div class="box green-bg"></div>
<div class="box yellow-bg"></div>
</div>
<script>
$(".box").click(function () {
// Select the element that
// is clicked on
let curr_elem = $(this);
// Set the amount to increase
let increase_modifier = 1.5;
// Get the current width of the element
let curr_width = curr_elem.width();
// Get the current height of the element
let curr_height = curr_elem.height();
// Use the same methods to set
// the new dimensions
curr_elem.height(
curr_height * increase_modifier
);
curr_elem.width(
curr_width * increase_modifier
);
});
</script>
</body>
</html>
Output:
Approach 2: Using the css() method.
This is similar to the above approach. All the divisions on the page are first selected using a common selector and a click binding is applied to trigger the function to increase the size using the click() method. The division that is then currently clicked can then be found out by using this as the selector.
The current height and width of the element can be found out using by using the css() method and passing the first parameter as "height" and "width". This will return the current height and width of the division. These are stored temporarily in separate variables after parsing them to an integer using the parseInt() method. The css() method is again used to assign the new height and width bypassing the new value as the second parameter. The new values can be calculated by multiplying the current height and width with a multiplier, similar to the above method.
Syntax:
$(".box").click(function () {
// Select the clicked element
let curr_elem = $(this);
// Get the current dimensions
let curr_width = parseInt(curr_elem.css("width"), 10);
let curr_height = parseInt(curr_elem.css("height"), 10);
// Set the CSS value of the element
curr_elem.css({
// Set the new height and width
width: curr_width * increase_modifier,
height: curr_height * increase_modifier,
});
});
Example:
HTML
<html>
<head>
<script src=
"https://code.jquery.com/jquery-3.3.1.min.js">
</script>
<style>
.container {
display: flex;
}
.box {
height: 100px;
width: 100px;
margin: 10px;
}
.blue-bg {
background-color: blue;
}
.green-bg {
background-color: green;
}
.orange-bg {
background-color: orange;
}
</style>
</head>
<body>
<h1 style="color: green">
GeeksforGeeks
</h1>
<b>
How to increase the size of a
division when you click it using jQuery?
</b>
<p>Click on a div to increase its size</p>
<div class="container">
<div class="box blue-bg"></div>
<div class="box green-bg"></div>
<div class="box orange-bg"></div>
</div>
<script>
$(".box").click(function () {
// Select the element that
// is clicked on
let curr_elem = $(this);
// Set the amount to increase
let increase_modifier = 1.5;
// Get the current width of the element
// and parse the value to integer
let curr_width =
parseInt(curr_elem.css("width"), 10);
// Get the current height of the element
// and parse the value to integer
let curr_height =
parseInt(curr_elem.css("height"), 10);
// Set the CSS value of the element
curr_elem.css({
// Set the new height and width
// after multiplying
width: curr_width * increase_modifier,
height: curr_height * increase_modifier,
});
});
</script>
</body>
</html>
Output:
Similar Reads
How to increase the width of a div by specified pixels once it is clicked using jQuery ? In this article, we will learn how to increase the width of a division by specified pixels once it is clicked using jQuery. We can do this task using the width() method that is used to return and set the width of an element in JQuery. So when we click on it a function is called in which first we sto
2 min read
How to get styles of a clicked division using jQuery ? In this article, we will learn how to get the styles (width, height, text color, and background color) of a clicked division using jQuery. Approach: The css() method is used to get the CSS property values of the selected element in jQuery. We use the css() method in jQuery that takes an array of nam
2 min read
How to get the background color of a clicked division using jQuery ? In this article, we will learn how to get the background color of a clicked division using jQuery. Approach: All the divisions on the page are first selected using a common selector and a click binding is applied to trigger the color detection using the click() method. The division that is then curr
2 min read
How to dynamically set the height and width of a div element using jQuery ? Set the height of a div element using jQueryThe content height of a div can be dynamically set or changed using height(), innerHeight(), and outerHeight() methods depending upon the user requirement. If the user wants to change the content height of a div dynamically, it includes changing the actual
7 min read
How to make font-size that expands when we resize the window using jQuery ? In this article, we will learn how to increase or decrease the font-size of elements in HTML when the window is resized using jQuery. This can be used in situations where the font size has to be responsive according to the width of the browser window.Approach: We will be using the jQuery css(), widt
2 min read