JavaScript Question-Answer

  1. Why the code is not working?
    <html>
        <body>
            <input id="billinput" type="number" placeholder="0" />
    
            <script>
                const billInput = document.getElementById('billinput');
                function cat() {
                    let billInputValue = billInput.value;
                    console.log(billInputValue);
                }
                cat();
            </script>
        </body>
    </html>
  2. Answer: use value instead of placeholder, also you can add optional button to call the function
    <html>
    <body>
    <input id="billinput" type="number" value="0" />
    <input type="button" value="Click" onclick="cat()">

    <script>
    const billInput = document.getElementById('billinput');
    function cat() {
    let billInputValue = billInput.value;
    console.log(billInputValue);
    }
    cat();
    </script>
    </body>
    </html>