Description:
The jQuery innerWidth () method is used to GET/SET the inner widthย for the matched element. This will includes left and right padding but not border/margin.
- GET Inner Width: It will return the InnerWidthย for the selected/matched element
- SET Inner Width: It will setย the InnerWidthย for the selected/matched element
Syntax
- $(selector).InnerWidth()
- $(selector).InnerWidth(value)
- $(selector).InnerWidth(value,function(Integer index, Number Width))
Valueย | It specify the number of pixels or a number along with an optional unit. |
function(Integer index,Number height | A function returning the inner height (including padding but not border) to set. The index will return the position of the element for the matched element and the Previous inner Widthย as arguments. |
When calling .innerWidth(“value”), the value can be either a string (number and unit) or a number. If only a number is provided for the value, jQuery assumes a pixel unit.
Example:ย
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
<html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"> </script> <style> .defaultClass { border: 5px solid #eed5b7; padding: 10px; width: 90px; height: 50px; background-color: red; display: inline-block; } </style> <script type="text/javascript"> $(document).ready(function () { $('.btnClass').click(function () { $('#divCurrentWidth').text(" Div width (no padding) : " + $('#divbox1').width()); $('#divInnerWidth').text(" Div Inner Width (with top & bottom padding) : " + $('#divbox2').innerWidth()); }) }); </script> </head> <body> <button class="btnClass" id="addMethod">SET/GET width() CSS property</button><br /><br /> <div id="divbox1" class="defaultClass"> </div> <div id="divbox2" class="defaultClass"> </div> <br /><br /> <div id="divCurrentWidth"></div> <div id="divInnerWidth"></div> </body> </html> |
Output:ย
Here, widthย of the box is 90. So width() method will return the same widthย mentioned the property but innerWidth() method will add the left(10px) & right(10px) padding ย and return 110 as its innerWidth.
Leave A Comment