Javascript - Convert String To Uppercase and Lowercase

Submitted by: 
Language: 
Visitors have accessed this post 136 times.

In this tutorial we will create a Convert String To Uppercase and Lowercase using Javascript. JavaScript is a scripting or programming language that allows you to implement complex things on web pages. It can renders web pages in an interactive and dynamic fashion. It is widely used in designing a stunning website. This code can be used as your converter to your mathematical problem. So Let's do the coding...

Getting Started:

This is the link for the bootstrap that has been used for the layout of the calculator https://getbootstrap.com/.

The Main Interface

This code contains the interface of the application. This code will render application and display the form. 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 href="<a href="https://sourcecodester.com"" rel="nofollow">https://sourcecodester.com"</a> 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">Javascript - Convert String To Uppercase / Lowercase</h3>
  15. <hr style="border-top:1px dotted #ccc;"/>
  16. <div class="col-md-3"></div>
  17. <div class="col-md-6">
  18. <form>
  19. <div class="form-group">
  20. <label>Enter a String</label>
  21. <input type="text" class="form-control" id="string"/>
  22. </div>
  23. <div>
  24. <input type="radio" name="status" value="first"/>
  25. <label>First Letter</label>
  26. <input type="radio" name="status" value="all" style="margin-left:50px;"/>
  27. <label>All</label>
  28. </div>
  29. <br />
  30. <center><button type="button" class="btn btn-primary" onclick="UpperCase()"><span class="glyphicon glyphicon-arrow-up"></span> Uppercase</button> | <button type="button" class="btn btn-danger" onclick="LowerCase()"><span class="glyphicon glyphicon-arrow-down"></span> Lowercase</button></center>
  31. </form>
  32. </div>
  33. </div>
  34. </body>
  35. <script src="js/script.js"></script>
  36. </html>

Creating the Script

This code contains the script of the application. This code will change the string format when clicked. To do this just copy write the code as shown below inside the text editor and save it as script.js.

  1. var string = document.getElementById("string");
  2.  
  3. function UpperCase(){
  4. if(GetRadio() == "all"){
  5. string.value = string.value.toUpperCase();
  6. }else if(GetRadio() == "first"){
  7. string.value = UpperCaseFirstLetter(string.value);
  8. }
  9. }
  10.  
  11. function LowerCase(){
  12. if(GetRadio() == "all"){
  13. string.value = string.value.toLowerCase();
  14. }else if(GetRadio() == "first"){
  15. string.value = LowerCaseFirstLetter(string.value);
  16. }
  17. }
  18.  
  19.  
  20. function GetRadio(){
  21. for (var i = 0; i < document.getElementsByName('status').length; i++){
  22. if (document.getElementsByName('status')[i].checked)
  23. {
  24. return document.getElementsByName('status')[i].value;
  25. }
  26. }
  27. }
  28.  
  29. function UpperCaseFirstLetter(string) {
  30. return string[0].toUpperCase() + string.slice(1);
  31. }
  32.  
  33.  
  34. function LowerCaseFirstLetter(string) {
  35. return string[0].toLowerCase() + string.slice(1);
  36. }

There you have it we successfully created a Convert String To Uppercase and Lowercase using javascript. I hope that this very 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.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.