Toggle the visibility of a second checkbox based on the checked and unchecked state of an iCheck checkbox

Code snippet for checkbox styling

<script>
$(function () {
    //Initialize Select2 Elements
    $(".select2").select2();        
    //Applying flat red color scheme for iCheck
    $('input[type="checkbox"].flat-red, input[type="radio"].flat-red').iCheck({
        checkboxClass: 'icheckbox_flat-red',
        radioClass: 'iradio_flat-red'
    });

});

Jquery script for toggling second checkbox

<script>
$("#ch_bx_haveparent").iCheck('toggle', function () {
    $("#ch_bx_haveparent").on('ifChecked', function (event) {
        alert('ch_bx_status Checkbox Shown');
        $("#ch_bx_status").show();
    });
    $("#ch_bx_haveparent").on('ifUnchecked', function (event) {
        alert('ch_bx_status Checkbox  hidden');
        $("#ch_bx_status").hide();
    });
});

Link to Icheck Checkbox on GitHub

Embedded HTML snippet

<div class="modal" id="menuiconmodal" tabindex="-1" role="dialog" aria-labelledby="myLargeModalLabel">
      <div class="modal-dialog">
        <div class="modal-content">
          <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal" aria-label="Close">
            <span aria-hidden="true">&times;</span></button>
            <h4 class="modal-title">Menu</h4>
          </div>
          <div class="modal-body"> 

       <div class="form-group"><input type="checkbox" class="flat-red" id="ch_bx_haveparent"/> 
       // Input tag for checkbox to be shown and hidden
       <div class="form-group"><input type="checkbox" class="flat-red" runat="server" id="ch_bx_status" /></div>
       </div>
          </div>
          <div class="modal-footer">

            <a href="javascript:;" id="sucess" class="btn btn-primary" data-dismiss="modal">Save</a>
          </div>
        </div>
        <!-- /.modal-content -->
      </div>
      <!-- /.modal-dialog -->
    </div>  
    <!-- /.modal -->

Alerts are displaying but the hide/show functionality is not working as expected.

Answer №1

$("#ch_bx_haveparent").change(function() {
  if ($("#ch_bx_haveparent").is(':checked')) {
    $("#ch_bx_status").show();
  } else {
    $("#ch_bx_status").hide();
  }

}).change();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<div class="modal" id="menuiconmodal" tabindex="-1" role="dialog" aria-labelledby="myLargeModalLabel">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
        <h4 class="modal-title">Menu</h4>
      </div>
      <div class="modal-body">

        <div class="form-group">
          <input type="checkbox" class="flat-red" id="ch_bx_haveparent" />// Input tag for checkbox that will be shown and hidden
          <div class="form-group">
            <input type="checkbox" class="flat-red" runat="server" id="ch_bx_status" />
          </div>
        </div>
      </div>
      <div class="modal-footer">

       
      </div>
    </div>
    <!-- /.modal-content -->
  </div>
  <!-- /.modal-dialog -->
</div>
<!-- /.modal -->

Give this method a try

Answer №2

$('#ch_bx_haveparent').on('change', function () {
    if ($(this).prop("checked")) {
        $("#ch_bx_status").fadeIn();
        return;
    }
    $("#ch_bx_status").fadeOut();
});

Answer №3

**Successfully Resolved Issue Using jQuery's addClass() and removeClass() Methods for toggling icheck checkbox visibility in HTML**

CSS Code Snippet

.call
    {
        display:none;
    }

jQuery Code Sample

<script>
$("#ch_bx_haveparent").iCheck('toggle', function () {
    $("#ch_bx_haveparent").on('ifChecked', function (event) {
        $("#parentmenu").removeClass("call"); // hide

    });
    $("#ch_bx_haveparent").on('ifUnchecked', function (event) {
        $("#parentmenu").addClass("call"); // shown
    });
});

**Located within div with id="parentmenu" **

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

How to vertically center a div using CSS

I need assistance center aligning #container with the following code: <div id="container"> <p>new</p> </div> The CSS provided is as follows- #container { position:relative; width:100%; background:red; padding:10px; ...

Back from the depths of the .filter method encased within the .forEach

I have been working on this code and trying to figure it out through trial and error: let _fk = this.selectedIaReportDiscussedTopic$ .map((discussionTopic) => {return discussionTopic.fk_surveyanswer}) .forEach((fk) => { ...

Exploring ElectronJs: The journey to sending messages from ipcMain to IpcRender and awaiting a response

I need help with sending a message to ask the renderer to parse an HTML string mainWindow.webContents.send('parse html', { resp}) The renderer processes the data and sends a reply ipc.on('parse html',function(e,p){ let b ...

Why are my API routes being triggered during the build process of my NextJS project?

My project includes an API route that fetches data from my DataBase. This particular API route is triggered by a CRON job set up in Vercel. After each build of the project, new data gets added to the database. I suspect this might be due to NextJS pre-exe ...

Is selectpicker acting up?

Currently, I am troubleshooting a filter feature on a website that utilizes jQuery with JSON data. Everything was functioning properly until recently when an error started appearing: The selectpicker function is not recognized I would greatly appreciat ...

What is preventing this PHP script from functioning properly with Google Maps?

I'm having trouble understanding why this PHP script isn't generating the specified map on the HTML page. Any suggestions? <!doctype html> <html> <head> <meta name="viewport" content="initial-scale=1.0, user-sc ...

Main navigation category as parent and secondary category as a child menu in WordPress

Hello there, I am looking to create a navigation menu with parent categories displayed horizontally and child categories as corresponding submenus. For example, The parent categories would be Apple, Orange, and Mango Under Apple, we would have apple1, ...

Error: Attempting to access a property called 'name' on an undefined variable leads to a TypeError

I am a beginner with MongodB and nodejs. I have successfully implemented the GET method, which returns an empty array. However, when I tried to use POST in Postman for "categories," I encountered this error message: ExpressJS categories route app.js Err ...

Sending the selected object from a dropdown in React back to its parent component

I have encountered a few issues with my React dropdown component. My main goal is to pass the selected object from the dropdown back to the Parent component. Currently, the dropdown list is functional and I am able to pass {this.state.selectedUser} back to ...

What is the best way to simultaneously send JavaScript variable and form data in Django?

I am trying to send a JavaScript variable to a Django view using the ajax() method. Below is my code: $('#orderDetails').submit(function() { // catch the form's submit event $.ajax({ type: 'POST', ...

Leveraging jQuery alongside HTML5 Video

I have a website that offers live streaming services. I wrote some code specifically for iPads which successfully plays the stream: window.location = 'http://<?php echo DEVSTREAMWEB; ?>/<?php echo $session_id;?>/'+camerahash+'/p ...

HTML - Table Layout Overlap

I'm struggling with a table layout issue in HTML and could use some assistance. The table below is displaying as overlapping in IE (please refer to the image), but it appears correctly in FF. Can anyone offer some guidance? <div> <table> ...

Convert coordinates from X and Y within a rotated ImageOverlay to latitude and longitude

After going over all the details in Projection's documentation and conducting various trial and error tests, I have hit a roadblock in finding a solution to this particular issue. All the solutions I have come across assume the x and y coordinates ar ...

Warning: The username index is not defined in the file C:xampphtdocsindex.php at line 4

Hey there, I just wanted to express my gratitude for taking the time to read this. It's all part of a college assignment (web server scripting unit p4) where I am working on setting up a simple login system. Unfortunately, I keep running into an error ...

Navigating Google Oauth - Optimal User Sign in Locations on Frontend and Backend Platforms

What are the distinctions between utilizing Google OAuth versus implementing user sign-ins at the frontend of an application, as opposed to handling them on the backend? For instance, managing user authentication in React to obtain the ID and auth object ...

What is the best way to adjust the height and width of a div when it comes into view through scrolling?

How can I automatically and frequently change the size of my div when it is scrolled into view? HTML <div id="contact" class="fadeInBlock"> <div class="map"> <div class="pointer"> <div class="dot"></div& ...

Retrieve the quantity of files in a specific directory by implementing AJAX within a chrome extension

I need assistance with obtaining the count of images in a specific directory using JS and AJAX within my chrome extension. My current code is included below, but it does not seem to be functioning as expected since the alert is not displaying. main.js .. ...

What is the best way to implement loading a script after an AJAX request has

var currentTallest = 0, currentRowStart = 0, rowDivs = new Array(), $el, topPosition = 0; $('.blocks').each(function() { $el = $(this); topPosition = $el.position().top; if (currentRowStart != topPosition) { // we just came to a new row. ...

Having trouble with CORS in your Angular application?

I am attempting to retrieve data from a site with the URL: Using the $http service After extensive research, I have come up with this CoffeeScript code snippet: angular.module('blackmoonApp') .controller 'PricingCtrl', ($scope, $ht ...

What is the best way to create 7 or 8 column grids with Vuetify's v-row and v-col components?

I understand that vuetify's grid system is based on a 12-column flex-box layout, but I would like to customize it to have 7 or 8 columns by default instead of the usual 12. In the code snippet below, you can see my attempt: <v-row> <v-col ...