Create a weight converter with Javascript

Hello Everyone, Today we’re going to create a Weight Converter with the help of basic JavaScript. If you’re a beginner then this article may be useful for you.
Our Weight Converter will look like this:-

For this project we use Bootstrap classes. I…


This content originally appeared on DEV Community and was authored by Rohit Sharma

Hello Everyone, Today we're going to create a Weight Converter with the help of basic JavaScript. If you're a beginner then this article may be useful for you.
Our Weight Converter will look like this:-

For this project we use Bootstrap classes. If in any case you don't know about Bootstrap then no problem you can easily style this web page with CSS from scratch.

Add the following code within the <head> .

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/css/bootstrap.min.css" integrity="sha384-rwoIResjU2yc3z8GV/NPeZWAv56rSmLldC3R/AZzGRnGxQQKnKkoFVhFQhNUwEyJ" crossorigin="anonymous">  

HTML

<body>
    <div class="container">
        <div class="row">
            <div class="col-md-6 offset-md-3">
                <h1 class="display-4 text-center mb-3">Weight Converter</h1>
                <form>
                    <div class="form-group">
                        <input type="number" class="form--control form--control-lg" placeholder="Enter Pounds....." id="lbsInput">
                    </div>
                </form>
                <div id="output">
                    <div class="card card-primary mb-2">
                        <div class="card-block">
                            <h4>Grams:</h4>
                            <div id="gramsOutput">

                            </div>
                        </div>
                    </div>
                    <div class="card card-success mb-2">
                        <div class="card-block">
                            <h4>Kilograms:</h4>
                            <div id="kgOutput">

                            </div>
                        </div>
                    </div>
                    <div class="card card-danger mb-2">
                        <div class="card-block">
                            <h4>Ounces:</h4>
                            <div id="ozOutput">

                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

CSS

body{
            margin-top: 70px;
            background: #333;
            color: #ffffff;

        }
        #output{
            visibility: hidden;
        }
        input[type=number]{
            width: 100%;
        }

JavaScript

This part is also easy. Our code contains only 10 lines of code.

document.getElementById('lbsInput').addEventListener('input',function(e){
            let lbs= e.target.value;
            let input = document.getElementById('lbsInput');
            let data = input.value;
            document.getElementById('gramsOutput').innerHTML= lbs/0.0022046;
            document.getElementById('kgOutput').innerHTML= lbs/2.2046;
            document.getElementById('ozOutput').innerHTML= lbs*16;
            document.getElementById('output').style.visibility='visible';

            if (data =='') {
                document.getElementById('output').style.visibility='hidden';
            }
        });

I hope you will love it ♥. If you love it then support me.

Buy Me A Coffee


This content originally appeared on DEV Community and was authored by Rohit Sharma


Print Share Comment Cite Upload Translate Updates
APA

Rohit Sharma | Sciencx (2021-12-09T15:57:28+00:00) Create a weight converter with Javascript. Retrieved from https://www.scien.cx/2021/12/09/create-a-weight-converter-with-javascript/

MLA
" » Create a weight converter with Javascript." Rohit Sharma | Sciencx - Thursday December 9, 2021, https://www.scien.cx/2021/12/09/create-a-weight-converter-with-javascript/
HARVARD
Rohit Sharma | Sciencx Thursday December 9, 2021 » Create a weight converter with Javascript., viewed ,<https://www.scien.cx/2021/12/09/create-a-weight-converter-with-javascript/>
VANCOUVER
Rohit Sharma | Sciencx - » Create a weight converter with Javascript. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/12/09/create-a-weight-converter-with-javascript/
CHICAGO
" » Create a weight converter with Javascript." Rohit Sharma | Sciencx - Accessed . https://www.scien.cx/2021/12/09/create-a-weight-converter-with-javascript/
IEEE
" » Create a weight converter with Javascript." Rohit Sharma | Sciencx [Online]. Available: https://www.scien.cx/2021/12/09/create-a-weight-converter-with-javascript/. [Accessed: ]
rf:citation
» Create a weight converter with Javascript | Rohit Sharma | Sciencx | https://www.scien.cx/2021/12/09/create-a-weight-converter-with-javascript/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.