In this example I’ll show you how to calculate area and perimeter of a rectangle in Jquery.
Result:
Jquery Code:
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 34 35 36 37 38 39 40 41 42 | <!doctype html> <html> <head> <title>Code4Examples</title> <meta charset="utf-8"> <style> label{ display: block; } </style> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.js"></script> </head> <body> <h1>Area and Perimeter of Rectangle</h1> <h2 id="area"></h2> <h2 id="perimeter"></h2> <label for="len">Length: <input type="text" id="len"> </label> <label for="wid">Width: <input type="text" id="wid"> </label> <button id="calcBtn">Calculate</button> <script> $("#calcBtn").bind('click',function(){ var len=Number($("#len").val()); var wid=Number($("#wid").val()); //area formule length*width $("#area").html("Area of rectange:"+len*wid); $("#perimeter").html("Perimeter of rectange:"+2*(len+wid)); }); </script> </body> </html> |