html - How to make radio buttons that show and hide inputs work within a jquery cloned div? -


i radio selection work within cloned div. div cloned indefinitely, each clone requires new selection.

how radio selection buttons within each div point items within div?

as stands now, selection clones when different items selected nothing happens.

please let me know if more information needed.

html:

<form> <div id="clonedform_1" class="clonedform">     <div class="addsomejazz">     <label name="btn1">selection 1         <input type="radio" name="select" class="btn1" value="selection 1" />     </label>     <label name="btn2">selection 2         <input type="radio" name="select" class="btn2" value="selection 2" />     </label>     <label name="btn3">selection 3         <input type="radio" name="select" class="btn3" value="selection 3" />     </label>     </div>     <br />     <div class="selection1 hidden" id="selection1_1">         <label name="text1" class="hideme">something thoughtful:             <input type="text" name="text1" class="hideme" />         </label>     </div>     <div class="selection2 hidden" id="selection2_1">         <label name="select1" class="hideme">select 1 of these:             <select class="hideme">                 <option>select this</option>                 <option>or this</option>                 <option>or maybe this</option>                 <option>do not select this</option>             </select>         </label>     </div>     <div class="selection2 hidden" id="selection3_1">         <label name="checkyboxy" class="hideme">what applies:             <input type="checkbox" name="thechecks" value="cool" class="hideme" />i cool             <input type="checkbox" name="thechecks" value="interesting" class="hideme" />i interesting             <input type="checkbox" name="thechecks" value="funny" class="hideme" />i funny</label>     </div> </div> <input type="button" id="cloneme" value="add entry" /> <input type="button" id="deleteme" value="get rid of last one" /> 

jquery:

$(document).ready(function () { $(function () {     $('#cloneme').click(function () {         var num = $('.clonedform').length,             newnum = new number(num + 1),             newelem = $('#clonedform_' + num).clone().attr('id', 'clonedform_' +         newnum).fadein('slow');         newelem.find('.selection1').attr('id', 'selection1_' + newnum);         newelem.find('.selection2').attr('id', 'selection2_' + newnum);         newelem.find('.selection3').attr('id', 'selection3_' + newnum);         newelem.find(":input[type='radio']").attr('name', 'select' + newnum);         newelem.find(".hideme").addclass('hidden');          $('#clonedform_' + num).after(newelem);         $('#deleteme').attr('disabled', false);     });     $('#deleteme').click(function () {         var num = $('.clonedform').length;         $('#clonedform_' + num).slideup('slow', function () {             $(this).remove();             if (num - 1 === 1) $('#deleteme').attr('disabled', true);         });     });      $('#deleteme').attr('disabled', true); });  $('.btn1').click(function () {     var num = $('.clonedform').length;      $('#selection1_' + num).show();     $('#selection2_' + num).hide();     $('#selection3_' + num).hide(); }); $('.btn2').click(function () {     var num = $('.clonedform').length;     $('#selection1_' + num).hide();     $('#selection2_' + num).show();     $('#selection3_' + num).hide(); }); $('.btn3').click(function () {     var num = $('.clonedform').length;     $('#selection1_' + num).hide();     $('#selection2_' + num).hide();     $('#selection3_' + num).show(); }); }); 

here link example set on fiddle: http://jsfiddle.net/katiekatie/pqd8s/

thanks!

the main problem event handlers. changed use event delegation , traversal functions find element hide , show. there few other small changes, , there still other optimizations done (for example, don't think dealing ids gaining anything).

http://jsfiddle.net/p9ykt/

here's important part:

$(document).on('click', '.btn1', function () {     var $container = $(this).closest('.clonedform');      $container.find('.selection1').show();     $container.find('.selection2').hide();     $container.find('.selection3').hide(); }); $(document).on('click', '.btn2', function () {     var $container = $(this).closest('.clonedform');      $container.find('.selection1').hide();     $container.find('.selection2').show();     $container.find('.selection3').hide(); }); $(document).on('click', '.btn3', function () {     var $container = $(this).closest('.clonedform');      $container.find('.selection1').hide();     $container.find('.selection2').hide();     $container.find('.selection3').show(); }); 

Comments

Popular posts from this blog

html - Sizing a high-res image (~8MB) to display entirely in a small div (circular, diameter 100px) -

java - IntelliJ - No such instance method -

identifier - Is it possible for an html5 document to have two ids? -