Ways to verify an iCheck located on the following page

Here is the code snippet I am currently working with:

$("#add-new-size-grp").click( function (event) {
    event.preventDefault();
    $.ajax({
        type: "get",
        url:"ajax-get-sizes.php",
        success: function(result){
            $("#sizegrp-table").html(result);
            var sizegrpSizesTable = $('#tbl-sizegrp-sizes').DataTable();
            //iCheck for checkbox and radio inputs
            $('input[type="checkbox"]').iCheck({
              checkboxClass: 'icheckbox_polaris',
              radioClass: 'iradio_polaris'
            });

            $("#modalNewSizeGrp").modal();
        }
    });
});

This piece of code fetches data and creates a jQuery DataTable. The DataTable is then inserted into a DIV element on the current page.

In the section that starts with

$('input[type="checkbox"]').iCheck({
, I am using the iCheck plugin to style the checkboxes in the first column of the table.

While this method works perfectly for the initial set of data, any new checkboxes displayed when switching pages or adjusting the page length do not retain the custom styling.

I have attempted to address this issue by applying the formatting upon the page.dt event using the following code:

$('#tbl-sizegrp-sizes').on( 'page.dt', function () {
    var dtable = $('#tbl-sizegrp-sizes').DataTable();
    var info = dtable.page.info();
    $('input[type="checkbox"]').iCheck({
      checkboxClass: 'icheckbox_polaris',
      radioClass: 'iradio_polaris'
    });

});

However, I seem to be missing something. Any guidance on what could be causing this issue?

-- EDIT --

Upon request, here is a demonstration of the problem in a JSFiddle. Take note of the styled checkboxes on the first page and observe how they change when navigating to other pages.

Answer №1

ROOT CAUSE

One possible reason for the issue is that jQuery DataTables removes non-visible elements from the DOM. When applying the iCheck() function, it may only affect elements on the first page.

RESOLUTION

To address this issue, utilize the drawCallback option to specify a function to execute after the table is drawn.

var sizegrpSizesTable = $('#tbl-sizegrp-sizes').DataTable({
   'drawCallback': function(settings){
      //Implement iCheck for checkbox and radio inputs
      $('input[type="checkbox"]').iCheck({
         checkboxClass: 'icheckbox_polaris',
         radioClass: 'iradio_polaris'
      });
   }
});

DEMONSTRATION

Refer to this jsFiddle link for the code sample and live demonstration.

ADDITIONAL RESOURCES

For further examples and in-depth information, explore jQuery DataTables: Custom control does not work on second page and beyond article.

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

Sorting functionality malfunctioning after dynamically adding new content using AJAX

Greetings to all, I have a question about my views. 1- Index 2- Edit In the index view, I have a button and AJAX functionality. When I click the button, it passes an ID to the controller which returns a view that I then append to a div. The Edit view con ...

PHP script encounters error while trying to modify CSS class

On my sticky login form, I am encountering an issue where the input fields should turn red when submitted with blank information. Interestingly, some fields behave correctly while others exhibit random behavior. The left photo below displays the initial lo ...

Align Text Center inside a Magical H1 Heading Tag

I'm having a bit of trouble with something that should be simple. I want to center the text inside my h1 tag on a wizard, and I've added this CSS to my stylesheet.css: .h1textalign { text-align:center; } Here's how I'm trying to apply ...

Expansive Offspring Division stretching to the Entire Vertical Length of its Guardian Division

I'm attempting to achieve full coverage of the parent div section by my child div section. Take a look at the Example URL (specifically where the Canadian Flag Stand Up Paddle Boarders are located) towards the bottom: Essentially, when I set the widt ...

The dimensions of the box are not predetermined by the size of the photo

I'm attempting to develop a photo gallery that emulates the style of (using the Unsplash API -> ) However, the size of the container box does not adjust properly with the photos. <div className="imageGrid__container"> <di ...

"Design the website with a WYSIWYG editor while preventing users from disrupting the page's

I am considering using an HTML WYSIWYG editor like CKEditor, but I am concerned about the possibility of users submitting HTML code that could alter the layout of the page when rendered. Here is a comparison between two posts: <p><b>This is m ...

Issues with Disabling the Browser's Back Button with jquery

I have been attempting to prevent the back button from functioning in a specific Browser, Microsoft Bing. Interestingly, my code works only if I click somewhere in the window first. If I don't click anywhere and try to go back directly, it still allow ...

How to properly utilize $.ajaxComplete and $.ajaxError functions?

I have been utilizing the code snippet below for my ajax form submissions: $.ajax({ url: "", data: ilmoittautumisdata, type: "POST", success:function(){ }); error:function(){ }); }); However, I recall being advised to discontinue using success and erro ...

Click to make the arrow come to life through animation!

I am brand new to coding and this is my inaugural code snippet: .container_menu { position: relative; top: 0px; left: 0px; width: 25px; height: 25px; } .container_menu .line-1 { background-color: black; width: 59%; height: 2px; positio ...

Issue with populating dropdown menu inside jquery modal dialog box

When I click on the 'create new button', a modal window form pops up with a dropdown menu. The dropdown is supposed to be populated via ajax with the help of the populateUserData() function. Even though the ajax call seems to be successful, I am ...

Sending a picture through AJAX using the camera feature of p5.js

Currently, I am exploring the camera functionality of p5.js to capture video. However, I am facing a challenge when trying to upload these images to a server using ajax. I am unsure about how to convert a p5.js Image object into a suitable format for trans ...

Using Lazy Load Plugin for jQuery to enhance Backbone.js functionality

I am working on a Backbone.js application using require.js and underscore.js. I am trying to incorporate the jquery lazy loading plugin with the Eislider banner. The Eislider banner was functioning properly before implementing the lazy loading script. Th ...

jQuery's slideDown function adds to the height of the page before smoothly scrolling it down

Every time I click on #download, the #musicDownload div smoothly slides down and expands the page height. However, I'm struggling to make the page scroll all the way down to the beginning of that div after it's expanded. This is the current code ...

DataTables prioritizes sorting strings over numerical values

I am currently utilizing jquery.datatables to display numbers in columns within datatables. The numbers are formatted with spaces between thousands units (e.g. 123 456 789). However, this formatting causes the sorting to treat the numbers as strings rather ...

Delete a table row based on the values of checkboxes using Jquery

I've successfully generated an array from values selected in an HTML form using checkboxes. Is there a way to pass these selected checkbox values to jQuery and utilize the code $('tr#myTableRow').remove(); for each value in the array submit ...

What is the best way to stop a jQuery event listener on a button within a form from triggering upon the user hitting the Enter key?

Here is an example of a form containing a button: <form> <input type="text"> <button>Click Me</button> <input type="submit" value="Submit"/> </form> Using the following JavaScript: $('form').subm ...

Floating DIVs wrapped in a responsive layout

I can't help but wonder if I have a layout like this: <div class="container"> <div class="left"> Left </div> <div class="right> Right </div> </div> Changing the view port to 320 pixels requires the right div to ap ...

Div or box container with a CSS arrow sliced through

Is there a way to transition from a basic square menu to something more complex like this -> view image example Below is the CSS I've been using: .menu { width:50px; height:50px; display:block; background-color:red; overflow:hidden -ms-transform: ...

Is it possible to update the page title with JQuery or JavaScript?

Is it possible to modify the page title using a tag inside the body of the document with jQuery or JavaScript? ...

C# MVC - Enabling dynamic row addition in an HTML table with multiple fields consolidated into a single cell

Hello, I'm new to c# MVC and could use some guidance. Currently, I am utilizing an example from Matt Lunn (here) to dynamically add new items to a bound list within an HTML table. However, the issue is that all the new items are being added to the fir ...