Selecting checkboxes in two separate divs by class name using jQuery

Here is a link to a fiddle showcasing my current project: http://jsfiddle.net/393Yk/

In my layout, I have two columns containing nested unordered lists with numerous checkboxes.

The objective is for the checkboxes in the right column to mirror the selection of checkboxes in the left column - essentially synchronizing their states.

The desired functionality involves only displaying elements on the right side once they have been selected on the left side.

I am encountering an issue where the right hand side checkboxes do not consistently update after the initial selection/unselection process.

The code snippet responsible for handling checkbox manipulation is as follows:

$('.mod-left .wf-check').attr('checked', false).change(function(){

    var sel = $(this).data("val");

    if( $(this).is(":checked") ){

        console.log("Checked " + sel);

        $('.mod-right input.wf-check-' + sel + '[type=checkbox]').attr('checked', true);

    } else {

        console.log("Unchecked " + sel);

        $('.mod-right input.wf-check-' + sel + '[type=checkbox]').attr('checked', false);

    }
});

I would appreciate any assistance in efficiently selecting and manipulating checkboxes based on their class name. Thank you.

Answer №1

This method appears to be effective without relying on the jquery attr() function.

if( $(this).is(":checked") ){
    $('.mod-right input.wf-check-' + sel + '[type=checkbox]')[0].checked = true;
} else {
    $('.mod-right input.wf-check-' + sel + '[type=checkbox]')[0].checked = false;
}

An alternative approach would be to write it more concisely:

$('.mod-right input.wf-check-' + sel + '[type=checkbox]')[0].checked = $(this).is(':checked');

Answer №2

Make changes wherever necessary

$('.mod-right input.wf-check-' + sel + '[type=checkbox]').attr('checked',...);

to

$('.mod-right input.wf-check-' + sel + '[type=checkbox]').prop('checked',...);

Fiddle

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

Fixing SCRIPT1028 error in IE9 with the help of AngularJS

Utilizing AngularJS in my single-page application, I have a button defined as follows: <button class="btn btn-success" onclick="doSeekTo({{todo.position}});doPlay();"> When this button is clicked, it triggers the javascript function with the specif ...

What is the best way to utilize two values in Vue.js 2?

When I attempt to structure my component in the following way: <script> export default { template: '\ <select class="form-control" v-on:change="search">\ <option v-for="option in option ...

Using JSON.stringify() for custom serialization of an object

Imagine there is an object called a with properties: const a = { foo: 123, bar: 'example' } Now, this object is a part of another object called b like this: const b = { a: a, anotherField: "example" } While working with TypeScript, th ...

Mix up and present cards for a game of blackjack (Javascript)

Have you noticed why "card2" is randomly adding an object to the array? It should consistently add objects to the array. const cards=[ { card: '&#127137', value: '1' }, { card: '&#127138', valu ...

Convert this JavaScript function into a jQuery function

I currently have this JavaScript function: function removeStyle(parent){ var rmStyle = document.getElementById(parent).getElementsByTagName("a"); for (i=0; i<rmStyle.length; i++){ rmStyle[i].className = ""; } } Since I am now using ...

Mastering the art of nesting loops and conditions in React: A comprehensive guide

I am fetching data in React using the following method: const data = [ { header: 'my header1', copy: [ 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.', 'Etiam et risus quam. Pr ...

Exploring Webgl Shaders with Three.js

Currently, I am working on creating custom vertex and fragment shader programs for a three.js project. My focus is on implementing a specific feature (custom clipping plane), which requires me to modify the shaders already provided by three.js. However, ...

Ways to make video controls visible following the initial click on the video

I have a collection of videos on my website and I would like them to display with a poster (thumbnail image) initially. The video controls should only appear once the user clicks on the video for the first time. Therefore, when the page is loaded, the cont ...

Avoid Race Condition in High-Traffic Transactions in Express JS with MongoDB

After successfully creating an Online Shop Website using Mongo DB and Express.js, I implemented flash sale features that caused a significant increase in traffic during the Flash Sale event, reaching up to 5,000 users per minute. However, a major issue ar ...

Tips for pressing the enter key to submit when faced with two buttons

I am developing a form with two input fields and their respective submit buttons. I want users to be able to enter text into either field, hit the Enter key, and have it trigger the same action as clicking the submit button. Currently, pressing Enter after ...

What is the precise mechanism behind the functioning of rails respond_to feature?

Currently, I'm in the process of developing an application that requires a remote form feature. As I work on this, I've realized that I have never fully grasped how Rails' respond_to function operates. Let's consider the code snippet b ...

The alignment of text in the btn-check label of Bootstrap is not centered

My checkboxes with btn-check have labels of varying lengths. I want all the labels to be aligned in the middle, but I can't figure out how to do it. The screenshot below illustrates the issue with the first checkbox. The content is not centered in the ...

Animating the height of a div using nested div elements

Below is the code snippet provided: https://jsfiddle.net/wc48jvgt/69/ The situation involves a div with a horizontal layout named #cleaning_card_1, containing embedded divs that act as borders, and a vertical div called #cleaning_card_right_1 with a neste ...

Issue with Webpack: file-loader failing to load SVG files dynamically in HTML

Configuration in package.json: { "name": "webpackTutorial", ... "devDependencies": { "bootstrap": "^4.3.1", ... "webpack-merge": "^4.2.2" } } Webpack Configuration in webpack.common.js: var HtmlWebpackPlugin = ...

What is the best way to extract the content from a unique div class on a webpage?

I need help extracting the content from this section a substantial amount of information Here is the code I tried using url = ('https://osu.ppy.sh/users/1521445') page = requests.get(url, headers=headers) soup = BeautifulSoup(page.con ...

Experiencing a [$compile:multidir] error when attempting to implement a multiselect dropdown with Angular.js

I encountered an issue when utilizing a multi-select drop-down list in Angular.js. Error: angularjs.js:107 Error: [$compile:multidir] http://errors.angularjs.org/1.4.6/$compile/multidir?p0=ngDropdownMultiselec…22%20checkboxes%3D%22tr ...

Implementing Java script for onkeypress event in a search bar that does not require a button

Currently utilizing this javascript to trigger an event on key press: function myFunction() { if (window.event || window.event.keyCode == 13) { changeSource(); } } function changeSource(){ document.getElementById("frame1").src="Log-In. ...

Creating an element in JavaScript with a designated ID is a

I'm working on creating a JavaScript array that holds 3 elements: var employees = [ {"firstName":"John", "lastName":"Doe"}, {"firstName":"Anna", "lastName":"Smith"}, {"firstName":"Peter", "lastName":"Jones"} ] To iter ...

Using jQuery's $.post() method with complete URLs

I'm currently working on a website that has three separate branches - dev, stage, and production. Each branch runs on its own unique URL: prod: stage: dev: To ensure that my code runs seamlessly across all three branches without the need for file ...

What is the method for invoking a function with a chosen date in the @syncfusion schedule with Angular 9?

capture of calendar display I am looking for a way to trigger a function with the selected date as a parameter. However, I am unable to locate the appropriate event to use within this module. Can someone provide assistance with this issue? Thanks! ...