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 names of properties and return an array of values of that properties. We store this array and use it for displaying the CSS of all the clicked div’s.
var styles = $(this).css([ "width", "height", "color", "background-color", ]);
Example: The following code demonstrates the above approach. Refer to the output for better understanding. On click of one div, it shows the width, height, color, and background-color properties and their respective values.
- Before clicking on div:
- After clicking on div: