Cheap VPS & Xen Server


Residential Proxy Network - Hourly & Monthly Packages

jQuery innerWidth()


jQuery innerWidth() method is used to return the inner width of the first matched

element without including border and margin.This method includes padding but excludes border and margin.

This image explains that jQuery innerWidth () method includes padding but not border and margin.

Syntax:

  1. $(selector).innerWidth()

Example of jQuery innerWidth() method

Let’s take an example to demonstrate the jQuery innerWidth() method.

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <script src=“https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js”></script>
  5. <script>
  6. $(document).ready(function(){
  7.     $(“button”).click(function(){
  8.         alert(“Inner width of div is: ” + $(“div”).innerWidth());
  9.     });
  10. });
  11. </script>
  12. </head>
  13. <body>
  14. <div style=“height:100px;width:500px;padding:10px;margin:3px;border:1px solid blue;background-color:lightpink;”></div><br>
  15. <button>Click here to get the inner width of the div</button>
  16. </body>
  17. </html>

Test it Now

jQuery innerWidth() example 2

Let’s take an example to demonstrate the effect of jQuery innerWidth() method.

  1. <!DOCTYPE html>
  2. <html>
  3.    <head>
  4.       <title>The jQuery Example</title>
  5.       <script type=“text/javascript” src=“http://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js”></script>
  6. <script type=“text/javascript” language=“javascript”>
  7.          $(document).ready(function() {
  8.             $(“div”).click(function () {
  9.                var color = $(this).css(“background-color”);
  10.                var width = $(this).innerWidth();
  11.                $(“#result”).html(“Inner Width is <span>” + width + “</span>.”);
  12.                $(“#result”).css({‘color’: color, ‘background-color’:’white’});
  13.             });
  14.          });
  15.       </script>
  16. <style>
  17.          #div1{ margin:10px;padding:10px; border:2px solid #666; width:60px;}
  18.          #div2 { margin:15px;padding:15px; border:4px solid #666; width:60px;}
  19.          #div3 { margin:20px;padding:20px; border:6px solid #666; width:60px;}
  20.          #div4 { margin:25px;padding:25px; border:8px solid #666; width:60px;}
  21. </style>
  22. </head>
  23. <body>
  24. <p>Click on any square:</p>
  25. <span id=“result”> </span>
  26. <div id=“div1” style=“background-color:orange;”></div>
  27. <div id=“div2” style=“background-color:green;”></div>
  28. <div id=“div3” style=“background-color:brown;”></div>
  29. <div id=“div4” style=“background-color:violet;”></div>
  30. </body>
  31. </html>

Comments

comments