Skip to content
Related Articles
Open in App
Not now

Related Articles

How to use javascript function in check box?

Improve Article
Save Article
  • Difficulty Level : Expert
  • Last Updated : 08 Feb, 2022
Improve Article
Save Article

Checkboxes are used for instances where a user may wish to select multiple options, such as in the instance of a “check all that apply” question, in forms. HTML Checkboxes Selected. A checkbox element can be placed onto a web page in a pre-checked fashion by setting the checked attribute with a “yes” value. 

  • Typically shaped as square.
  • Allow the user to select options with a single click.
  • Options share a single name.
  • Checkbox allow you to select more than one options per group
    • HTML Code: HTML document by employing the dedicated HTML tag that wraps around JavaScript code. The tag can be placed in the section of your HTML, in the section, or after the close tag, depending on when you want the JavaScript to load. 


<!DOCTYPE html>
<html lang="en">
        <meta charset="UTF-8" />
        <meta name="viewport"
                content="width=device-width, initial-scale=1.0" />
        <link rel="stylesheet" href=
        <div class="container">
            <div class="row">
                <div class="col-md-3"></div>
                <div class="col-md-6">
                            <div class="form-group">
                                <label for="yesgraduated">
                                    Are you graduated?
                                <input id="yesgraduated"
                                   type="checkbox" value="yes"
                                 onchange="graduatedFunction()" />
                            <br />
                            <div id="graduated"
                                <label for="degreename">
                                    Degree Name:
                                <input type="text"
                                    id="degreename" />
                                <br />
                            <button type="button"
                                    class="btn btn-primary"
                <div class="col-md-3"></div>

  • JavaScript Code: Now, in the javaScript code we’re trying to give the option of including the degree in the form. The user need to tell if he/she is graduated or not. So when we click on this checkbox, a new input field opens up. So if someone clicks or doesn’t click it, the “graduatedFunction” function will either display the hidden part or display none. 


    function graduatedFunction() {
        if (document.getElementById("yesgraduated")
            .style.display = "inline";
            .setAttribute("required", true);
        } else {
            .style.display = "none";

My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!