Using jQuery to create a Select All Checkbox that toggles a specific class

I have come across similar examples in the past, but I have been unable to make it work as intended. The examples I found only use standard checkboxes. I attempted to customize the checkbox using a sprite sheet by applying a class, but I am struggling to apply the concepts from those examples to my specific case.

Here is the code snippet:

<div id="showHideAll"">Show/Hide All<input type="checkbox" id="allCheck" name="pinSet" class="pinToggles" onclick="checkAllLinks()">
    <label for="allCheck" class="css-label"></label></div>
</div>

<div>Opt1<input type="checkbox" id="opt1Check" name="pinSet" class="pinToggles" onclick="checkOpt1Links()">
    <label for="opt1Check" class="css-label"></label>
</div>

<div>Opt2<input type="checkbox" id="opt2Check" name="pinSet" class="pinToggles" onclick="checkOpt2Links()">
     <label for="opt2Check" class="css-label"></label>
</div>

<div>Opt3<input type="checkbox" id="opt3Check" name="pinSet" class="pinToggles" onclick="checkOpt3Links()">
     <label for="dinShopCheck" class="css-label"></label>
</div>

The appearance of the sprite changes based on the 'checked' property using a css class.

input[type=checkbox].pinToggles:checked + label.css-label {
background-position: 0 -16px;
}

Most of this code pertains to other functionalities, but I included it for reference purposes.

This is how I structured the individual checkboxes:

function checkOpt1Links(){
    $('#opt1 li a').toggleClass("inactive");

 if(opt1.getVisible() == true)
    {       
        opt1.setOptions({visible:false});
    }
    else
    {
        opt1.setOptions({visible:true});
    }
}

I am trying to achieve the common functionality of a 'select all' checkbox where checking it selects all boxes and unchecking it deselects them, but also clicking any box when the select all is checked should uncheck both the select all and the clicked checkbox and vice versa. I referenced this example: Jquery "select all" checkbox but I am struggling to implement it correctly. Thank you for any assistance!

Answer №1

1) Opt for the change event listener instead of using click when dealing with checkbox and radio buttons.

You can simplify it like this:

$('#toggleShowHide').on('change', 'input[type=checkbox]', function () {
    if ($('.checkToggles').is(':checked')) {
        $('.checkToggles').prop('checked', false)
    }
    else {
    $('.checkToggles').prop('checked', true)
    }
});

2) The class and onclick event handler have been removed from the following checkbox.

HTML:

<div id="toggleShowHide">Toggle Show/Hide<input type="checkbox" id="toggleCheck" name="toggleSet" >
    <label for="toggleCheck" class="style-class"></label></div>
</div>

Take a look at this JSFiddle

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 should white space be managed in Bootstrap 4 cards when incorporating responsive column classes - wrap elements inside divs or apply classes directly?

In Bootstrap 4, when applying a responsive column class like "col-sm-8" directly to a "div" with the class "card", whitespace is added between the card border and content. This can be seen in the highlighted yellow area in the image below. https://i.sstat ...

Is there a glitch or a misinterpretation of the specifications concerning the use of relative padding

VIEW DEMO At times, I like to create a square (or any rectangle) that maintains its ratio regardless of size, using a technique similar to this method. My Objective: To prevent the square from extending beyond the viewport on devices with limited heigh ...

Verify that the input is zero and then proceed to deactivate the submit button if it is indeed zero in AngularJS

app.controller('HelloController', function($scope) { console.log($scope.input1); console.log($scope.input2); if (($scope.op_option == 4) && ($scope.input2 == 0)) { myForm.$invalid; } }); <form id="calc" method="pos ...

Develop an array using a variable's value in JavaScript

I need assistance with creating an array of variable length based on a dynamic value ranging from 1 to 15. The goal is to populate the array differently depending on the specific value of the variable. For instance, if the variable's value is 1, I wo ...

Bringing in a JavaScript file into a Svelte component

Today, I stumbled upon Svelte and I am really intrigued by the concept. However, I encountered an issue while attempting to import a small helper.js file. No matter what I try, whenever I reference the class, I receive the error: ReferenceError: Helper ...

Prevent input element from being included in tab order in Internet Explorer

I’m facing an issue in my Single Page Application built with vue.js. I have a checkbox that needs to be invisible for UX reasons, so I set its opacity to zero. However, even with tabindex set to -1, the input element is still included in the tab order ...

Utilize React-select in Nextjs to dynamically alter URLs through options

During my attempt to implement react-select for changing the URL based on user-selected multiple options to filter numbers, I encountered a challenge. When using multiple options, the URL changes successfully to fetch data, as seen in this example: https:/ ...

Utilize jQuery to extract data from a Steam page in JSON format

Although I have researched extensively on this topic, it is still not functioning as desired. I came across this Steam page in JSON format. My aim is to extract the lowest_price variable without using PHP as I am incorporating it into my Chrome extension. ...

What could be the reason behind the failure of my inner div to successfully implement the flex-row

I'm currently trying to position two divs next to each other horizontally, and my concept was to utilize flexbox, but including the property flex-row doesn't seem to yield any results and I can't comprehend the reason behind it. How can I a ...

Issue: The plugin 0 mentioned in the file "/my dir/node_modules/babel-preset-php/src/index.js" contains an invalid property called "default"

While attempting to convert a PHP script to JavaScript using babel-preset-php, I encountered the following error: Error: Plugin 0 specified in "/media/deep/5738c180-2397-451b-b0b5-df09b7ad951e1/deepx/Documents/TestingAll/node_modules/babel-preset-php/ ...

The login function in Nativescript Vue successfully returns a true value

Hello, I am currently using a function that is quite similar to this one with some minor tweaks here. However, I have encountered an issue when trying to log in to my REST API. Even if I input the incorrect username or password, it still authenticates me ...

Searching for a streamlined approach to retrieve a segment of a string

I'm currently working with JavaScript and TypeScript. Within my code, I encountered a scenario where I have a string that might contain certain tags indicating importance or urgency. Here are a couple of examples: A: "Remind me to go to the store to ...

IE11 experiencing issues with font loading

I need help troubleshooting why the fonts are not loading properly in the body section of my articles, specifically when viewed in Internet Explorer. Take a look at an example article here: I am fetching from this CSS file: , and I have included the nece ...

Display only the labels of percentages that exceed 5% on the pie chart using Chart JS

I'm currently diving into web development along with learning Chart.js and pie charts. In my project, the pie chart I've created displays smaller percentage values that are hardly visible, resulting in a poor user experience on our website. How c ...

Ajax - Is utilizing API Endpoints the correct approach?

I am encountering an encoding issue when making an AJAX request to an API Endpoint. The code snippet below shows the Endpoint implementation using Java Spring: @Autowired ApiKeyRepository apiKeyRepository; @RequestMapping(value= "/weather/{cit ...

The URL in $.mobile.changePage has not been updated

One of the challenges I encountered was switching between two pages - a menu list page and a menu detail page. The transition from the menu to the menu detail page was achieved using $.mobile.changePage. While everything seemed to be working correctly, the ...

Activating an Ajax-filled dropdown when the previous dropdown remains unchanged but a selection has been made

I am working with three dropdowns: the first one is populated from the server, the second one is filled via ajax based on a change in the first dropdown, and the third one is also filled via ajax based on a change in the second dropdown. Below is the code ...

jQuery problem causes page to scroll when button is clicked

Is there a way to smoothly scroll to the second section of the page when a button is clicked using jQuery? $('.home_scroll_btn a').on('click', function(){ var scrollPosition = $('#intro-home').offset().top; $( ...

What is the best way to generate an extensive table with HTML and AngularJS?

I am looking to implement an expandable table feature where a single header with a plus icon can be clicked to reveal the child rows beneath it. Currently, I have been able to achieve this functionality. However, I am facing an issue where the child rows ...

Only initiate a reload once the Post request has been successfully completed

$('.active-per').click(function () { var text = $(this).data('value'); $('.active-per').removeClass('active'); if (text == "t" || text == "n" || text == "c" || text == "r") { $(this).addClass("ac ...