jQuery width Method


jQuery width Method : is used to get or set the width of selected element. If want to set width of element pass width in .width() as input parameter.
We are going to explain jquery width method with example and demo.


jQuery width Method Syntax 1 Get Width

In this example we will get width of element using .width() method.

$(selector).width();

jQuery width() Method Example 1

Below example shows the get width method functionality –

<script type="text/javascript">
         $(document).ready(function(){ 
              $( "#widthExample" ).on( "click", function(event) {             
                  var x = $("#myDiv").width();       
                   alert(x+"px");            
              });
               
          });
</script>

Try it »

jQuery width Method Syntax 2 – Set Width

If you want to add width dynamically you can use width method as below-

$(selector).width(width);

width(px): Width in pixel.

jQuery width() Method Example 2

Here is an example of set width method –

<script type="text/javascript">
         $(document).ready(function(){ 
              $( "#widthExample" ).on( "click", function(event) {             
                $("#myDiv").width(400);               
              });
               
          });
</script>

Try it »


Advertisements

Add Comment

📖 Read More