• Hello, Visitor
  • Monday, February 17, 2025
Search Result
Home » Javascript » BMI Calculator with Javascript

BMI Calculator with Javascript

Video Tutorial:



Html Code:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>BMI Calculator</title>

    <!-- Bootstrap Css -->
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.6.1/dist/css/bootstrap.min.css">
</head>
<body class="bg-info">
   
    <div class="container mt-5">
        <div class="row justify-content-center">
            <div class="col-lg-5 col-md-7 mt-5">
                <div class="card mt-5">
                    <div class="card-header text-center">
                        <h1>BMI Calculator</h1>
                    </div>
                    <div class="card-body">
                        <form action="">
                            <div class="form-group">
                                <label for="" class="font-weight-bold">Height (CM):</label>
                                <input type="text" name="" id="height" class="form-control" placeholder="Enter height">
                            </div>
                            <div class="form-group">
                                <label for="" class="font-weight-bold">Weight (KG):</label>
                                <input type="text" name="" id="weight" class="form-control" placeholder="Enter weight">
                            </div>
                            <div class="form-group text-center mt-4">
                                <button class="btn btn-primary">Calculate</button>
                            </div>
                        </form>

                        <div class="row">
                            <div class="col-12 mt-4 text-center">
                                <h4>Result: <span id="results">0</span></h4>
                            </div>
                        </div>

                        <div class="row">
                            <div class="col-12 mt-4 text-left">
                                <h6 class="mb-3 text-success"><strong>BMI Weight Guide</strong></h6>

                                <p class="m-0"><strong>Under Weight:</strong> Less Than 18.6</p>
                                <p class="m-0"><strong>Normal Weight:</strong> 18.6 to 24.9</p>
                                <p class="m-0"><strong>Over Weight:</strong> Greater Than 24.9</p>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>



    <!-- Bootstrap Scripts -->
    <script src="https://cdn.jsdelivr.net/npm/jquery@3.5.1/dist/jquery.slim.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.1/dist/umd/popper.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@4.6.1/dist/js/bootstrap.min.js"></script>

    <script src="script.js"></script>
</body>
</html>


Javascript Code:

const form = document.querySelector('form');

form.addEventListener('submit', function(e){
    e.preventDefault();

    const height = parseInt(document.querySelector('#height').value);
    const weight = parseInt(document.querySelector('#weight').value);
    const result = document.querySelector('#results');

    if(height === '' || (height < 0) || (isNaN(height))){
        result.innerHTML = "Please enter a valid height";
    }
    else if(weight === '' || (weight < 0) || (isNaN(weight))){
        result.innerHTML = "Please enter a valid weight";
    }
    else{
        const bmi = (weight / ((height*height)/10000)).toFixed(2);

        result.innerHTML = bmi;
    }
})


Leave a Reply

You must Login to add a comment