Javascript examples for jQuery Method and Property:width
The width() method sets or gets the width of the selected elements.
Parameter | Require | Description |
---|---|---|
value | Required for setting width. | Sets the width in px, em, pt, etc. Default unit is px |
function(index,currentwidth) | Optional. | Sets a function that returns the new width of selected elements |
The following code shows how to Return the width of a <div> element:
<!DOCTYPE html> <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> <script> $(document).ready(function(){ $("button").click(function(){ console.log("Width of div: " + $("div").width()); });// w w w . j a v a 2s . co m }); </script> </head> <body> <div style="height:100px;width:300px;padding:10px;margin:3px;border:1px solid blue;background-color:lightblue;"></div><br> <button>Display the width of div</button> </body> </html>