jeudi 4 mai 2023

Handling Forms with JavaScript - Checkbox Validation

I'm currently studying JavaScript and still in beginner level. So, I referred given code from a certain Open Learning Platform. Its intention was to validate whether the checkbox is checked or not (that's how it was said in there). but it won't output any message. Could you help me with it.

<body>
    <script type="text/javascript">
        const form = document.getElementById('form')
        const checkBox = document.getElementById('checkbox')
        const message =document.getElementById('message')
        
        form.addEventListener('submit', function checkBoxValidator(x) {
            x.preventDefault()
            if (checkBox.checked === true) {
                message.innerHTML = 'Done'
            }
            else {
                message.innerHTML = 'Please Check Something'
            }
        })
    </script>

    <form id="form">
        <input type="checkbox" id="checkbox"> Checkbox
        <button type="submit">Submit</button>
        <h3 id="message"></h3>
    </form>
</body>

Thank You!

I tried refer some external resources as such and as far as my knowledge I cannot find where the error stands.




Aucun commentaire:

Enregistrer un commentaire