jqueryexample.com jquery width() example - jqueryexample.com

jquery width() example by jqueryexample

In jQuery,width() function returns the width of an element.
Syntax: $(selector).width( value );
If only a number is provided for the value, jQuery assumes a pixel unit.
Example1:
<html>
<head><script type="text/javascript">
$(document).ready(function(){
$("#getp").click(function () {
alert("width of paragraph: " + $("p").width());
});
$("#getd").click(function () {
alert("width of document: " + $(document).width());
});
$("#getw").click(function () {
alert("width of window: " + $(window).width());
});
});
</script>
</head>
<body>
<button id="getp">Get Paragraph width</button>
<button id="getd">Get Document width</button>
<button id="getw">Get Window width</button>
<div> </div>
<p> Sample paragraph to test width</p>
</body>
</html></head>
<body>
<button id="getp">Get Paragraph width</button>
<button id="getd">Get Document width</button>
<button id="getw">Get Window width</button>
<div> </div>
<p> Sample paragraph to test height
</body>
</html>

Example2:
$(“button”).click(function(){
$(this).width(30);
});

Example3:
$(‘p’).text(‘Browser (Width : ‘ + $(window).width() + ‘ )’);

Did you like this? Share it:

Discussion

No comments yet, be the first.

Leave a Comment