call plugins mehods for dynamically added element to DOM in jQuery

47 Views Asked by At

I need to call some jquery plugin method for new created and added elements to page.

Before I used livequery plugin to discover those elements and call my desired plugin for those. for example in this example I used livequery plugin to call iCheck plugin for new elements:

$('input[type="checkbox"], input[type="radio"]').livequery(function () {
    $(this).iCheck({
        checkboxClass: 'icheckbox_minimal-blue',
        radioClass: 'iradio_minimal-blue'
    })
});

This plugin was great and worked very well. but seems that is deprecated and is Not compatible with jQuery 3+ while I need to use latest vesrions of jQuery.

on the other hand I have many element like this that are added to page after load page.

Is there a similar method or similar plugin that is compatible with jQuery 3+ that can be used in the same way?

1

There are 1 best solutions below

1
Nader Hashemi On

You can replace it with on() event and select the parent element of the inserted element.

$('body').on(
'DOMNodeInserted','input[type="checkbox"],input[type="radio"]', 
   function() {    
     $(this).iCheck({
        checkboxClass: 'icheckbox_minimal-blue',
        radioClass: 'iradio_minimal-blue'
    }) 
});