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

Kilograms To Pounds Converter Using Javascript

awesomemc1

Email Campaign Revenue Specialist
A Rep
0
0
0
Rep
0
A Vouches
0
0
0
Vouches
0
Posts
171
Likes
97
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 300 XP
In this tutorial, we are going to learn Kilograms To Pounds Converter Using Javascript. This program numeric values only accepts in the textbox and not allow the characters by setting the code like this: <input

type

=

"number"

>

.

This simple program contains a textbox and button for the user to put the desired amount of Kilograms to Convert into Pounds after clicking the "Convert" button.

JavaScript Code

This script will execute the converting of kilograms to pound.

  1. <

    script>
  2. var

    button =

    document.getElementsByTagName

    (

    'button'

    )

    ;
  3. button[

    0

    ]

    .onclick

    =

    display_result;
  4. button[

    1

    ]

    .onclick

    =

    clear_text;
  5. document.onload

    =

    function

    (

    )

    {
  6. document.getElementById

    (

    'values'

    )

    .focus

    (

    )

    ;
  7. }

    ;
  8. function

    convert_to_pounds(

    number_value)
  9. {

  10. return

    (

    number_value *

    2.20462262

    )

    ;
  11. }
  12. function

    display_result(

    )

    {
  13. var

    kilograms =

    document.getElementById

    (

    "values"

    )

    .value

    ;
  14. var

    result =

    convert_to_pounds(

    kilograms)

    ;

  15. if

    (

    document.getElementById

    (

    "values"

    )

    .value

    ==

    ''

    )

    {
  16. alert(

    "Cannot be empty. Kindly type your value to convert."

    )

    ;
  17. document.getElementById

    (

    'values'

    )

    .focus

    (

    )

    ;

  18. }
  19. else

    {
  20. document.getElementById

    (

    'result'

    )

    .innerHTML

    =

    kilograms +

    " kilogram(s) is equal to "
  21. +

    result.toFixed

    (

    2

    )

    +

    " pound(s). "

    ;
  22. }
  23. }
  24. function

    clear_text(

    )

    {
  25. document.getElementById

    (

    "values"

    )

    .value

    =

    ""

    ;
  26. document.getElementById

    (

    "result"

    )

    .innerHTML

    =

    ""

    ;
  27. document.getElementById

    (

    "values"

    )

    .focus

    (

    )

    ;
  28. }
  29. function

    numberTo_Convert(

    evt)

    {
  30. evt =

    (

    evt)

    ?

    evt :

    window.event

    ;
  31. var

    charCode =

    (

    evt.which

    )

    ?

    evt.which

    :

    evt.keyCode

    ;
  32. if

    (

    charCode >

    31

    &&

    (

    charCode <

    48

    ||

    charCode >

    57

    )

    )

    {
  33. return

    false

    ;
  34. }
  35. return

    true

    ;
  36. }
  37. </

    script>

HTML Source Code

This simple code where the user input their numeric value to convert kilograms into pounds.

  1. <h2

    style

    =

    "text-align:center; color:blue;"

    >

    Kilograms To Pounds <br

    /

    >

    Converter Using Javascript</

    h2

    >

  2. <table

    width

    =

    "100%"

    cellpadding

    =

    "5"

    cellspacing

    =

    "5"

    >
  3. <tr

    >
  4. <td

    ><label

    >

    Enter a value</

    label

    ></

    td

    >
  5. <td

    ><input

    type

    =

    "number"

    id

    =

    "values"

    class

    =

    "txt_Number"

    onkeypress

    =

    "return numberTo_Convert(event)"

    required autofocus=

    "autofocus"

    ></

    td

    >
  6. </

    tr

    >
  7. <tr

    >
  8. <td

    colspan

    =

    "2"

    style

    =

    "text-align:center;"

    ></

    td

    >
  9. </

    tr

    >
  10. <tr

    >
  11. <td

    colspan

    =

    "2"

    style

    =

    "text-align:center;"

    >
  12. <button

    class

    =

    "btn_convert"

    >

    Convert</

    button

    >
  13. <button

    class

    =

    "btn_clear"

    >

    Clear</

    button

    >
  14. </

    td

    >
  15. </

    tr

    >
  16. </

    table

    >

  17. <div

    id

    =

    "result"

    ></

    div

    >

And, this is the style.
  1. <style type=

    "text/css"

    >
  2. body {
  3. margin

    :

    auto

    ;
  4. width

    :

    400px

    ;
  5. }
  6. td {
  7. text-align

    :

    center

    ;
  8. color

    :

    blue

    ;
  9. font-size

    :

    18px

    ;
  10. font-weight

    :

    bold

    ;
  11. font-family

    :

    cursive

    ;
  12. }
  13. .txt_Number

    {
  14. width

    :

    100px

    ;
  15. text-align

    :

    center

    ;
  16. border

    :

    blue

    1px

    solid

    ;
  17. font-size

    :

    25px

    ;
  18. background

    :

    aliceblue

    ;
  19. }
  20. .btn_convert

    {
  21. border

    :

    blue

    1px

    solid

    ;
  22. background

    :

    azure

    ;
  23. color

    :

    blue

    ;
  24. font-size

    :

    18px

    ;
  25. font-family

    :

    cursive

    ;
  26. padding

    :

    5px

    ;
  27. }
  28. .btn_clear

    {
  29. border

    :

    red

    1px

    solid

    ;
  30. background

    :

    azure

    ;
  31. color

    :

    red

    ;
  32. width

    :

    75px

    ;
  33. font-size

    :

    18px

    ;
  34. font-family

    :

    cursive

    ;
  35. padding

    :

    5px

    ;
  36. }
  37. #result

    {
  38. font-size

    :

    larger

    ;
  39. font-family

    :

    cursive

    ;
  40. text-align

    :

    center

    ;
  41. margin-top

    :

    70px

    ;
  42. border

    :

    blue

    1px

    solid

    ;
  43. line-height

    :

    80px

    ;
  44. color

    :

    red

    ;
  45. }
  46. </style>

Share us your thoughts and comments below. Thank you so much for dropping by and reading this tutorial post. For more updates, don’t hesitate and feel free to visit this website more often and please share this with your friends or email me at [email protected]. Practice Coding. Thank you very much.


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

449,708

322,558

322,567

Top