Facing problem with javascript code in html

hello :slightly_smiling_face: I am facing a little problem with using javascript with HTML, I have created a small form just for learning purpose, the thing I want to do is if the user checks the checkbox with id=“programmer” then an alert will appear on screen giving a greeting that “Wow, you are also a programmer like me!!” but it doesn’t seem to work. I have never encountered DOM elements with HTML & JS, it’s my first try so it would be nice if you could help me :slightly_smiling_face:
This is the HTML code with tag containing JS code -

<!DOCTYPE html>

    <title>Slam Book</title>

<body style="background-color: rgba(2, 29, 29, 0.884); font-family: 'Segoe UI', Tahoma, sans-serif; font-weight: 600;">

    <h2>Tell us something about you</h2>
        First Name: &nbsp <input type="text" name="firstNameField" value="Enter your first name" size="20"><br>
        Last Name: &nbsp <input type="text" name="lastNameField" value="Enter your last name" size="20"> <br>
    <p>Enter your age</p>
    <input type="number" name="ageField" value="Enter your age">
    <p>Enter your password</p>
    <input type="password" name="pass" maxlength="25">
    <p>Tell us more about you</p>
    <textarea type="comments" cols="43" rows="10">write here...</textarea>

    <p>Select your favourite color</p>
    <select name="colorChoose" size="4">
        <option selected>Red

    <p>Tell us your profession</p>
    <input type="radio" name="r1" value="student" checked>Student &nbsp
    <input type="radio" name="r1" value="teacher">Teacher &nbsp
    <input type="radio" name="r1" value="doctor">Doctor &nbsp
    <input type="radio" name="r1" value="programmer">Programmer

    <p>Tell us your hobbies</p>
    <input type="checkbox" name="c1" value="cricket" checked>Cricket &nbsp
    <input type="checkbox" name="c1" id="programmer" value="programming">Programming &nbsp
    <input type="checkbox" name="c1" value="football">Football &nbsp
    <input type="checkbox" name="c1" value="music">Music <br>

    <p>Tell something more about you in a .txt file and attach it here</p>
    <input type="file" name="file1" accept=".txt">

            <input type="submit" value="submit">
            <input type="reset" value="reset">

        if (document.getElementById("programmer").checked) {

            alert("Wow, you are also a programmer like me!!");


Hope you can help. Thanks!! :grinning:

When a web page is rendered by the browser, it reads and renders the code from top to bottom. So the JS code runs on page render. But at this point, the checkbox/radio button isn’t checked/selected

for your idea to work, you will need to add some kind of event/trigger to the checkbox which fires/executes JS code when the radio button is clicked

seems checkboxes have an onchange event, radio buttons don’t. So then I would say onclick is the way to go:



Thanks this really helped!!