JavaScript - Dynamically Modify Element Radius

In this tutorial we will create a Dynamically Modify Element Radius using JavaScript. This code will change the border radius size when user drag the input range horizontally. The code use oninput() to initiate a function that dynamically change the border radius when the range input have been slide to modify the properties of the binded element . 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 https://getbootstrap.com/.

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="container-fluid">
  9. <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester</a>
  10. </div>
  11. </nav>
  12. <div class="col-md-3"></div>
  13. <div class="col-md-6 well">
  14. <h3 class="text-primary">JavaScript - Dynamically Modify Element Radius</h3>
  15. <hr style="border-top:1px dotted #ccc;"/>
  16. <div class="col-md-3"></div>
  17. <div class="col-md-6">
  18. <div id="target" style="width:100%; height:300px; background-color:red; border:2px solid #000;"></div>
  19. <br /><br />
  20.  
  21. <div class="form-group">
  22. <label>Set Value</label>
  23. <input type="range" oninput="changeRadius(this);" value="0" min="0" max="150"/>
  24. </div>
  25. <div class="form-inline">
  26. <input type="text" id="slide_text" class="form-control" value="0" size="4" readonly="readonly"/>
  27. </div>
  28. </center>
  29. </div>
  30. </div>
  31. <script src="js/script.js"></script>
  32. </body>
  33. </html>

Creating the Script

This code contains the script of the application. This code will dynamically change the border radius when the range is moved. 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 changeRadius(slider){
  2. var input=document.getElementById("slide_text");
  3.  
  4. input.value=slider.value;
  5.  
  6. document.getElementById("target").style.borderRadius=slider.value+"px";
  7. }
There you have it we successfully created a Dynamically Modify Element Radius 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