• We just launched and are currently in beta. Join us as we build and grow the community.

How to Convert String to UpperCase and LowerCase in JavaScript

dopla

Anime Festival Organizer
D Rep
0
0
0
Rep
0
D Vouches
0
0
0
Vouches
0
Posts
33
Likes
92
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 400 XP
How to Convert String to UpperCase and LowerCase in JavaScript

Introduction

In this tutorial we will create a How to Convert String to UpperCase and LowerCase. This tutorial purpose is to teach how to change the string cases. This will tackle the overall process for changing of string cases. I will provide a sample program to show the actual coding of this tutorial.

This tutorial is very easy to understand just follow the instruction I provided and you can do it without a problem. This program can be use to your website if you want to change your string cases. I will try my best to give you the easiest way of creating this program Converting cases. So let's do the coding.

Before we get started:

This 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 display only textbox and buttons. 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. <body

    >
  8. <nav

    class

    =

    "navbar navbar-default"

    >
  9. <div

    class

    =

    "container-fluid"

    >
  10. <a

    href

    =

    "https://sourcecodester.com"

    class

    =

    "navbar-brand"

    >

    Sourcecodester</

    a

    >
  11. </

    div

    >
  12. </

    nav

    >
  13. <div

    class

    =

    "col-md-3"

    ></

    div

    >
  14. <div

    class

    =

    "col-md-6 well"

    >
  15. <h3

    class

    =

    "text-primary"

    >

    How to Convert String to UpperCase and LowerCase</

    h3

    >
  16. <hr

    style

    =

    "border-top:1px dotted #ccc;"

    /

    >
  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

    =

    "str"

    /

    >
  22. </

    div

    >
  23. </

    form

    >
  24. </

    div

    >
  25. <div

    class

    =

    "col-md-6"

    >
  26. <div

    >
  27. <input

    type

    =

    "radio"

    name

    =

    "status"

    value

    =

    "first"

    /

    >
  28. <label

    >

    Single</

    label

    >
  29. <input

    type

    =

    "radio"

    name

    =

    "status"

    value

    =

    "all"

    style

    =

    "margin-left:50px;"

    /

    >
  30. <label

    >

    All</

    label

    >
  31. </

    div

    >
  32. <br

    /

    >
  33. <button

    type

    =

    "button"

    class

    =

    "btn btn-default"

    onclick

    =

    "UpperCase()"

    >

    Uppercase</

    button

    >

    <button

    type

    =

    "button"

    class

    =

    "btn btn-default"

    onclick

    =

    "LowerCase()"

    >

    Lowercase</

    button

    >
  34. </

    div

    >
  35. </

    div

    >
  36. </

    div

    >
  37. </

    body

    >
  38. <script

    src

    =

    "script.js"

    ></

    script

    >
  39. </

    html

    >

Creating JavaScript Function

This is where the main function of the application is. This code will dynamically change the string cases from lower or upper. To do this just copy and write these block of codes inside the text editor and save it as script.js.
  1. let string =

    document.getElementById

    (

    "str"

    )

    ;

  2. function

    GetRadio(

    )

    {
  3. for

    (

    let i =

    0

    ;

    i <

    document.getElementsByName

    (

    'status'

    )

    .length

    ;

    i++

    )

    {
  4. if

    (

    document.getElementsByName

    (

    'status'

    )

    [

    i]

    .checked

    )
  5. {
  6. return

    document.getElementsByName

    (

    'status'

    )

    [

    i]

    .value

    ;
  7. }
  8. }
  9. }

  10. function

    UpperCaseFirstLetter(

    string)

    {
  11. return

    string[

    0

    ]

    .toUpperCase

    (

    )

    +

    string.slice

    (

    1

    )

    ;
  12. }


  13. function

    LowerCaseFirstLetter(

    string)

    {
  14. return

    string[

    0

    ]

    .toLowerCase

    (

    )

    +

    string.slice

    (

    1

    )

    ;
  15. }

  16. function

    UpperCase(

    )

    {
  17. if

    (

    GetRadio(

    )

    ==

    "all"

    )

    {
  18. string.value

    =

    string.value

    .toUpperCase

    (

    )

    ;
  19. }

    else

    if

    (

    GetRadio(

    )

    ==

    "first"

    )

    {
  20. string.value

    =

    UpperCaseFirstLetter(

    string.value

    )

    ;
  21. }
  22. }

  23. function

    LowerCase(

    )

    {
  24. if

    (

    GetRadio(

    )

    ==

    "all"

    )

    {
  25. string.value

    =

    string.value

    .toLowerCase

    (

    )

    ;
  26. }

    else

    if

    (

    GetRadio(

    )

    ==

    "first"

    )

    {
  27. string.value

    =

    LowerCaseFirstLetter(

    string.value

    )

    ;
  28. }
  29. }

In this code we first set the string variable with name string. We then create a method that will change the string cases called UpperCaseFirstLetter() and LowerCaseFirstLetter(). In the created method we set the string with a special function that will change the string to an uppercase or lower case accordingly.

Output:

How%20to%20Convert%20String%20to%20UpperCase%20and%20LowerCase%20in%20JavaScript%201.png


The How to Convert String to UpperCase and LowerCase 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 Convert String to UpperCase and LowerCase 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


Download
You must upgrade your account or reply in the thread to view hidden text.
 

452,292

323,340

323,349

Top