icheck icon indicating copy to clipboard operation
icheck copied to clipboard

Cloning iCheck checkboxes elements

Open Ig-R opened this issue 10 years ago • 15 comments

Hi, in my project i'm using iCheck and have a lot of checkboxes and radiobuttons. When clone(true,true) to duplicate all elements a new form with all elements appear but events are bind to first form, if i click a checkbox then checkbox from first form will change not in second one. Using clone once more now third form is bind with event to first one and second is working perfect. It will be very good if any ideas about cloning iCheck input boxes will be presented.

Ig-R avatar Sep 23 '14 13:09 Ig-R

I'll test that.

drgullin avatar Jan 12 '15 10:01 drgullin

Any news on this issue? I'm having the same problem. If yu need any help or more information for this problem to be solved just ask!

Thanks in advance (great plug-in btw!)

jjgumucio avatar Mar 09 '15 13:03 jjgumucio

Yes same issue here. Looking for a quick solution or will need to abandon icheck on this project.

MSCAU avatar Aug 03 '15 04:08 MSCAU

What I did to bypass thi problem was to use jquery.clone() [no params] and after the cloning process y re-initialize the icheck plugin. It takes a little while with forms that use lots of radio inputs, but it worked.

Hope it helps

jjgumucio avatar Aug 03 '15 11:08 jjgumucio

Hi Joaquim (jjgumucio), you can post your code to re-initialize the icheck plugin? I don't have success with: $("#checkbox").iCheck(); and $("input[type='checkbox'], input[type='radio']").iCheck({checkboxClass: 'icheckbox_square-grey',radioClass: 'iradio_square-aero'})

orivalde avatar Aug 05 '15 18:08 orivalde

@orivalde Heres the code I run after running $.clone:

var allRadios = dolly.find(':radio');
allRadios.iCheck({
  checkboxClass: 'icheckbox_square-green',
  radioClass: 'iradio_square-green',
});

** Note that "dolly" is the variable name where the cloned element is stored.

Hope this solves your problem!

jjgumucio avatar Aug 05 '15 18:08 jjgumucio

Thank you @jjgumucio. I had another problem that was solved initializing the iCheck plugin after my clone call.

orivalde avatar Aug 05 '15 19:08 orivalde

Hi,

I have the same problem but using @jjgumucio solution I get a duplicate button inside the cloned button.

Here's a code snippet after the clone process is finished:

<div style="position: relative;" class="iradio_flat checked">
    <div style="position: relative;" class="iradio_flat">
        <input style="position: absolute; top: -20%; left: -20%; display: block; width: 140%; height: 140%; margin: 0px; padding: 0px; background: rgb(255, 255, 255) none repeat scroll 0% 0%; border: 0px none; opacity: 0;" name="name[]" value="" type="radio">
        <ins style="position: absolute; top: -20%; left: -20%; display: block; width: 140%; height: 140%; margin: 0px; padding: 0px; background: rgb(255, 255, 255) none repeat scroll 0% 0%; border: 0px none; opacity: 0;" class="iCheck-helper"></ins>
    </div>
    <ins style="position: absolute; top: -20%; left: -20%; display: block; width: 140%; height: 140%; margin: 0px; padding: 0px; background: rgb(255, 255, 255) none repeat scroll 0% 0%; border: 0px none; opacity: 0;" class="iCheck-helper"></ins>
</div>

The radios work but the cloned radios are under the active ones (1-2px lower).

voitin avatar Aug 21 '15 11:08 voitin

@doubleb Could you be more specific about the problem?

  • Are you cloning the element?
  • Where are you adding/inserting/appending the cloned element?

Could you post a javascript snipet for the problem?

jjgumucio avatar Aug 21 '15 15:08 jjgumucio

@jjgumucio I am cloning a DIV that has a couple of radios/checkboxes, here's the code that I use for this:

$('body').on('click', '.duplicate', function(e) {
        e.preventDefault();
        var clone = $(this).parent().parent().clone();
        clone.insertAfter($(this).parent().parent());
        var allRadios = clone.find(':radio');
        allRadios.iCheck({ checkboxClass: 'icheckbox_flat', radioClass: 'iradio_flat', increaseArea: '20%' });
    });

And this is how I enable iCheck:

$(document).on('icheck', function() { $('input[type=checkbox], input[type=radio]').iCheck({ checkboxClass: 'icheckbox_flat', radioClass: 'iradio_flat', increaseArea: '20%' }); }).trigger('icheck');

UPDATE: I fixed it by also cloning the event handlers ( clone(true) ).

voitin avatar Aug 21 '15 15:08 voitin

Thanks, same problem occur in my project, this is very helpful.

RenukaB30 avatar Dec 16 '15 07:12 RenukaB30

@voitin How do it?

masadi avatar Nov 07 '16 20:11 masadi

@masadi Long time without using this plugin but, my 2 cents on it: you should clone a single radio input no a div containing several inputs.

Hope it helps!

jjgumucio avatar Nov 07 '16 21:11 jjgumucio

before clone , use $(obj).iCheck('destroy') remove icheck style, then clone element, last use $(obj).iCheck({...}) re-init icheck style.

dumyjob avatar Jul 25 '18 09:07 dumyjob

If you already have a clone, there seems there's no other option but to strip iCheck's wrapper, and reinitialize the input. This is how you can do that:

var cloneCheckbox = clone.querySelector('input[type="checkbox"]');
container.replaceChild(cloneCheckbox, clone);
//Call $(cloneCheckbox).iCheck with desired arguments, attach event listeners

Full working example at https://codepen.io/anon/pen/GzvBbx?editors=1010

developMediaSoft avatar Feb 05 '19 11:02 developMediaSoft