Having trouble with selecting checkboxes in a div using jQuery? While it may work in IE and Firefox, Chrome seems to be causing issues

In my div, I have several checkboxes placed under labels for formatting purposes. There is a list of checkbox names that need to be checked upon certain actions. Here is the list: var columns= ['2','5','4']

This is the current HTML structure:

 <div id="menu" class="dropdown-menu hold-on-click dropdown-checkboxes pull-right">
   <label><input type="checkbox" id="cbox0" data-column="0" name='1'>1</label>
   <label><input type="checkbox" id="cbox0" data-column="0" name='2'>2</label>
   <label><input type="checkbox" id="cbox0" data-column="0" name='3'>3</label>
   <label><input type="checkbox" id="cbox0" data-column="0" name='4'>4</label>
   <label><input type="checkbox" id="cbox0" data-column="0" name='5'>5</label>
   <label><input type="checkbox" id="cbox0" data-column="0" name='6'>6</label>
</div>

The code works in IE and Firefox, but not in Chrome. Any suggestions for an alternative approach or fixing the bug?

$('input', $('#menu')).each(function () {
                var chkBoxName = $(this).attr('name');
                 var selected = columns.indexOf(chkBoxName);
                 if(selected !== -1){
                  $(this).prop('checked', 'checked');
                  console.log(chkBoxName +  $(this).attr('checked'));
              }
            });

I found this JSFiddle that works in IE but not in Chrome. The issue seems to relate to checkboxes being nested within tables in some cases. Not sure how to resolve it on my end.

This is the problem I encountered with Chrome version 34:

Answer №1

Initially, there seems to be a few issues with your HTML code:

  • All of your checkboxes have identical IDs
  • Labels should not contain the input element within them

    <label for="cbox1">1</label>
    <input type="checkbox" id="cbox1" data-column="1" name="1">
    

Upon testing, the code appears to be functioning properly: http://jsfiddle.net/Lw9k9zd4/

Answer №2

 <div id="navigation" class="dropdown-menu hold-on-click dropdown-checkboxes pull-left">
       <label><input type="checkbox" id="box0" data-column="0" name = 'A'>A</label>
       <label><input type="checkbox" id="box1" data-column="0" name = 'B'>B</label>
       <label><input type="checkbox" id="box2" data-column="0" name = 'C'>C</label>
       <label><input type="checkbox" id="box3" data-column="0" name = 'D'>D</label>
       <label><input type="checkbox" id="box4" data-column="0" name = 'E'>E</label>
       <label><input type="checkbox" id="box5" data-column="0" name = 'F'>F</label>
    </div>
    <script>
    $(document).ready(function(e) {
        $("#navigation input").each(function(index, item) {
             $(this).prop('checked', 'checked');
        });

    });

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

Leveraging the power of Ajax and Javascript to handle and process conditional if/else statements

Hey there, I'm new around here so I hope I got this right. Currently, I'm diving into the Ajax for Dummies book after enjoying the PHP one years ago. However, I've hit a roadblock with my first real Ajax program. It took me ages to locate th ...

Having trouble with Vue.js image force download not functioning properly with anchor tags?

Currently, I am utilizing Laravel in conjunction with Vue.js. I have had the requirement to forcibly download an image on the browser, but unfortunately, it is not functioning as expected. var link = document.createElement('a'); link.href = &apo ...

Remove the opacity setting from a transparent box that covers the entire page

I find that real code tends to be more complex, but this snippet does a good job of highlighting the issue at hand. In my setup, there's a background video playing (in this case, just as the background), and on top of that sits a transparent watch fa ...

Unable to use href attribute as intended

HTML: <a id="Switch">Click here to switch</a> <a href="image1_large.png" class="mainA blade"> <img id="mainImage" src="image1.png"/></a> Javascript: <script> $(function() { $('#Switch').click(functio ...

Code for creating a multiselect box using jQuery

Looking to create a drop-down menu with multiple checkboxes similar to this example: Are there any other sources that offer something like this? ...

What is the best way to retrieve data from Elastic Search using Node.js?

I currently work with NODE JS in conjunction with an elastic search DB. Within this setup, I am utilizing the following package: https://www.npmjs.com/package/@elastic/elasticsearch In my elastic search DB, I have a collection that looks like this: [ { ...

What is the best way to retrieve distinct id values from a form in Rails when sending a DELETE request via AJAX?

I am determined to grasp the intricacies of AJAX and how it operates, hence, I prefer not to use "remote: true" for this task. The issue at hand is that despite attempting to send a DELETE request to the server using a form input, each submission results i ...

The absence of jasmine-node assertions in promises goes unnoticed

Everything seems to be running smoothly with the code below, except for the assertion part. Whenever I run the test using Jasmine, it reports 0 assertions. Is there a way to include my assertions within promises so they are recognized? it("should open sav ...

render previous and next buttons for slider dynamically

I'm currently using the iDangerous swiper to set up a basic carousel. My goal now is to have the prev and next buttons show up dynamically. For example, when you first start the slider only the next button is visible. Once you click, both the next and ...

repeating the identical content in the same setting

I am encountering an issue with jquery as I have 2 different sets of icons, each of which I want to do the same thing: My primary codes are <ul> <li> <a href=""> <i class="fa fa-facebook"></i> ...

I want to set a single background image for all slides in fullPage.js. How can I achieve this

I am attempting to replicate a similar effect as shown in this example: The demonstration above utilizes the fullPage.js plugin. You may have noticed how the background image remains consistent while navigating through different sections. I have searched ...

Adding a <tr> tag to an HTML table using JQuery and AJAX in the context of Django framework step by step

I am currently navigating the world of Javascript, Jquery, and Ajax requests and I'm encountering a challenge with how my scripts are executing. My homepage contains a lengthy list of items (over 1200) that need to be displayed. Previously, I loaded ...

Attempting to dynamically update the title of a Vue Meta page using a combination of a string and

In the blog application for my project using Nuxt JS 2.4.5, I am utilizing Vue Meta. I'm encountering difficulties while attempting to set the title along with a variable from data (), and it seems like something crucial is eluding me. Despite tryin ...

You should only call them after the method that returns a promise has completed

submitTCtoDB() { console.log("The selected file list contains: " + this.selectedFileList) this.readFile().then(() => { alert("ReadFile has finished, now submitting TC"); this.submitTC() }); } readFile() { return new Promise((resolve, r ...

Navigating between socket.io and express using while loops

Currently, I am running an express app with socket.io on my raspberry pi to control an LED panel. The panel is being driven by a while loop that updates the pixels. However, I am looking for a way to modify the parameters of this loop or even switch to a d ...

How can you verify the value of a disabled HTML input element in TestCafe using Typescript?

TestCafe Typescript - how to verify the value of a disabled HTML input element? Despite being disabled for user interaction, I want to ensure that this element still holds the anticipated value. example public async checksomething(text: string) { co ...

Stacking a Bootstrap column above another column

Is there a way to stack a bootstrap column on top of another column while maintaining its size and responsiveness? Consider this scenario with 4 columns arranged in a row: https://i.sstatic.net/qug0g.png Upon clicking the Overlay btn, the E column shoul ...

Next.js data response not found

My code seems to be having an issue where the data fetched is not displaying on my website. I can see the data when using console.log(data), but nothing shows up when using src={data.img1}. async function getData() { const res = await fetch("http:/ ...

Ways to extract text from a temporary element

Here is my HTML code: <div class="p-login-info" ng-show="loggedOut()">My text.</div> This is the corresponding JavaScript code: var e = element(by.className('p-login-info')); e.getText() .then(function(text){ var logoutText = ...

Is it considered good or bad practice to use plain JavaScript objects in an AngularJS application?

Imagine needing a custom object that doesn't rely on AngularJS (such as a specific collection with unique functionalities). You could create it independently of AngularJS and simply use it in services/controllers. Alternatively, you could design it a ...