Check whether a checkbox is checked with JavaScript. Show Table of Contents
Display some text when the checkbox is checked: Checkbox: Check Whether a Checkbox is CheckedStep 1) Add HTML:ExampleCheckbox: <input type="checkbox" id="myCheck" onclick="myFunction()"> <p id="text" style="display:none">Checkbox is CHECKED!</p> Step 2) Add JavaScript:Example function myFunction() { // If the checkbox is checked, display the output text Try it Yourself ยป Use the jQuery toggle() methodThe following example will demonstrate you how to show and hide div elements based on the selection of
checkboxes using the jQuery
</p> <span id="ezoic-pub-ad-placeholder-109"></span><span class="ezoic-ad ezoic-at-0 large-mobile-banner-1 large-mobile-banner-1109 adtester-container adtester-container-109" data-ez-name="laravelcode_com-large-mobile-banner-1"><span id="div-gpt-ad-laravelcode_com-large-mobile-banner-1-0" ezaw="580" ezah="400" style="position:relative;z-index:0;display:inline-block;padding:0;width:100%;max-width:1200px;margin-left:auto!important;margin-right:auto!important;min-height:400px;min-width:580px" class="ezoic-ad"><script data-ezscrex="false" data-cfasync="false" type="text/javascript" style="display:none">if(typeof ez_ad_units != 'undefined'){ez_ad_units.push([[580,400],'laravelcode_com-large-mobile-banner-1','ezslot_2',109,'0','0'])};__ez_fad_position('div-gpt-ad-laravelcode_com-large-mobile-banner-1-0'); </p></div> </div> <div class="card"> <div class="card-header d-flex align-items-center justify-content-between"> <h6 class="mg-b-0 tx-bold">Author : Harsukh Makwana</h6> </div> <div class="card-body"> <div class="media row"> <div class="col-md-2"> <div class="avatar d-flex align-items-center justify-content-center"> <img src="https://www.laravelcode.com/upload/author.jpg" alt="Harsukh Makwana" class="img-responsive rounded-circle w-100 pull-center"> </div> </div> <div class="col-md-10"> <div class="media-body pd-l-25"> <p class="mg-b-3"> Hi, My name is Harsukh Makwana. i have been work with many programming language like php, python, javascript, node, react, anguler, etc.. since last 5 year. if you have any issue or want me hire then contact me on <a target="_blank" href="https://laravelcode.com/cdn-cgi/l/email-protection#cfa7aebdbcbaa4a7fdfe8fa8a2aea6a3e1aca0a2"><span class="__cf_email__" data-cfemail="1971786b6a6c72712b28597e74787075377a7674">[email protected]</span></a> </p> <a href="https://github.com/Harsukh21" class="btn btn-outline-dark btn-icon" target="_blank"> <i class="fab fa-github"></i> </a> <a href="https://www.youtube.com/laravelcode" class="btn btn-outline-danger btn-icon" target="_blank"> <i class="fab fa-youtube"></i> </a> <a href="https://stackoverflow.com/users/4120409/harsukh-makwana" class="btn btn-outline-secondary btn-icon" target="_blank"> <i class="fab fa-stack-overflow"></i> </a> <a href="https://www.facebook.com/Laravelcodedemo/" class="btn btn-outline-primary btn-icon" target="_blank"> <i class="fab fa-facebook"></i> </a> <a href="https://twitter.com/laravelcode" class="btn btn-outline-info btn-icon" target="_blank"> <i class="fab fa-twitter"></i> </a> </div> </div> </div> <span id="ezoic-pub-ad-placeholder-112"></span> </div> </div> </div> <style type="text/css"> .list-group .avatar img { width: inherit; } .gsc-search-button-v2 { padding: 10px 15px; } </style> <div class="col-md-4 mg-t-10 mg-lg-t-0"> <div class="card"> <div class="card-header d-flex align-items-center justify-content-between"> <h6 class="mg-b-0 tx-bold">Search</h6> </div> <div class="card-body"> <script data-cfasync="false"> How do I show and hide div elements using checkboxes?Approach:. Selector name for checkbox is same as the element which is used to display the. content.. CSS display property of each element is set to none using display: none; for hiding the element initially.. Use . toggle() method for displaying and hiding the element for checking and unchecking the box.. How do I show and hide input fields based on checkbox?To show or hide the field, we are applying CSS through jQuery css() method. We are passing CSS display property. To hide the field, we set the display property to none and to show it we set the display property block. How do I hide a check box?There are several ways to hide the <input type="checkbox"> :. Use display: none.. Use visibility: hidden.. Use opacity: 0.. Position it off the screen using position: absolute and an insanely big value like left: -9999px.. How do you check checkbox is checked or not?Checking if a checkbox is checked First, select the checkbox using a DOM method such as getElementById() or querySelector() . Then, access the checked property of the checkbox element. If its checked property is true , then the checkbox is checked; otherwise, it is not. |