How to Automatically Change String Case in JavaScript

How to Automatically Change String Case in JavaScript

Introduction

In this tutorial we will create a How to Automatically Change String Case in JavaScript. This tutorial purpose is to teach you automatically change your string case. This will tackle the simple way of changing of string case. I will provide a sample program to show the actual coding of this tutorial.

This tutorial is simple and easy to understand just follow the instruction I provided and you can do it without a problem. This program can be use to any system or application that need some automation feature. I will give my best to provide you the easiest way of creating this program Change String Case Automatically. So let's do the coding.

Before we get started:

Here is the link for the template that i used for the layout design https://getbootstrap.com/.

Creating The Interface

This is where we will create a simple interface for our application. This code will only display the html forms and inputs. To create this simply copy and write it into your text editor, then save it 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 href="https://sourcecodester.com" class="navbar-brand">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">How to Automatically Change String Case</h3>
  15.                 <hr style="border-top:1px dotted #ccc;"/>
  16.                 <div class="col-md-6">
  17.                         <h3>UPPERCASE</h3>
  18.                         <div class="form-group">
  19.                                 <input type="text" class="form-control" onkeyup="changeToUpper(this.value)" placeholder="Enter here..."/>
  20.                         </div>
  21.                         <br />
  22.                         <h4>Result</h4>
  23.                         <div class="form-group">
  24.                                 <input type="text" class="form-control" id="all_upper" readonly="readonly"/>
  25.                         </div>
  26.                        
  27.                 </div>
  28.                 <div class="col-md-6">
  29.                         <h3>LOWERCASE</h3>
  30.                         <div class="form-group">
  31.                                 <input type="text" class="form-control" onkeyup="changeToLower(this.value)" placeholder="Enter here..."/>
  32.                         </div>
  33.                         <br />
  34.                         <h4>Result</h4>
  35.                         <div class="form-group">
  36.                                 <input type="text" class="form-control" id="all_lower" readonly="readonly"/>
  37.                         </div>
  38.                 </div>
  39.         </div>
  40. </body>
  41. <script src="script.js"></script>
  42. </html>

Creating JavaScript Function

This is where the main function of the application is. This code will allow you to change string case automatically. To do this just copy and write these block of codes inside the text editor and save it as script.js.
  1. function changeToUpper(string){
  2.                 if(string==""){
  3.                         document.getElementById('all_upper').value="";
  4.                        
  5.                 }else{
  6.                         document.getElementById('all_upper').value=string.toUpperCase();
  7.                        
  8.                 }
  9.                
  10. }
  11.  
  12. function changeToLower(string){
  13.         if(string==""){
  14.                         document.getElementById('all_lower').value="";
  15.                        
  16.                 }else{
  17.                         document.getElementById('all_lower').value=string.toLowerCase();
  18.                 }
  19. }

In the code above we just create two methods that will change the string cases from Upper to Lower. The first method is called changeToUpper(), this function will automatically change your inputted string into Upper cases. And the last method will be called changeToLower(), this function will automatically change the entered string to lower cases

Output:

The How to Automatically Change String Case in JavaScript source code that I provide can be download below. Please kindly click the download button.

There you have it we successfully created How to Automatically Change String Case in 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!

More Tutorials for JavaScript Language

JavaScript Tutorials

Add new comment