JavaScript - Dynamically Adjust Image Size

Submitted by: 
Visitors have accessed this post 170 times.

In this tutorial we will create a Dynamically Adjust Image Size using JavaScript. This code will change the image size when user click a button. The code use onclick() to initiate a function that will dynamically change the image size by binding the image id and set its element style with your desire size. Feel free to modify and apply it in your system, this is a user-friendly kind of program

We will be using JavaScript as a server-side scripting language because It gives a greater control of your web page and extend its capability in a modern way approach. It is written in HTML or as an external sourcing to add some necessary features in your website.

Getting started:

First you have to download bootstrap framework, this is the link for the bootstrap that I used for the layout design

The Main Interface

This code contains the interface of the application. To create this just write these block of code inside the text editor and save this as index.html.

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
  5. <link rel="stylesheet" type="text/css" href="css/bootstrap.css" />
  6. </head>
  7. <nav class="navbar navbar-default">
  8. <div class="containet-fluid">
  9. <a class="navbar-brand" href="<a href="">Sourcecodester</a>
  10. " rel="nofollow">">Sourcecodester</a>
  11. </a> </div>
  12. </nav>
  13. <div class="col-md-3"></div>
  14. <div class="col-md-6 well">
  15. <h3 class="text-primary">JavaScript - Dynamically Adjust Image Size</h3>
  16. <hr style="border-top:1px dotted #ccc;"/>
  17. <div class="col-md-4">
  18. <center><button class="btn btn-primary" onclick="resizeImage('large')">Large</button></center>
  19. <br />
  20. <center><button class="btn btn-success" onclick="resizeImage('medium')">Medium</button></center>
  21. <br />
  22. <center><button class="btn btn-danger" onclick="resizeImage('small')">Small</button></center>
  23. </div>
  24. <div class="col-md-8">
  25. <center><img src="image.jpeg" id="image" style="width:300px; height:200px;"/></center>
  26. </div>
  27. </div>
  28. <script src="js/script.js"></script>
  29. </body>
  30. </html>
  31. </html>

Creating the Script

This code contains the script of the application. This code will dynamically change the image size when the button is clicked. To do this just copy and write these block of codes inside the text editor, then save it as script.js inside the js folder.

  1. function resizeImage(str){
  2. if(str=="small"){
  3. document.getElementById('image').style.height="140px";
  4. document.getElementById('image').style.width="200px";
  5. }else if(str=="medium"){
  6. document.getElementById('image').style.height="200px";
  7. document.getElementById('image').style.width="300px";
  8. }else if(str=="large"){
  9. document.getElementById('image').style.height="350px";
  10. document.getElementById('image').style.width="100%";
  11. }
  12. }

There you have it we successfully created a Dynamically Adjust Image Size using JavaScript. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!

Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You may insert videos with [video:URL]
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe> [video]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.