php - Multiple javascript dropdown clones -


i trying use javascript clone , remove functions multiple times on page, 1 script. have 20 drop downs should have function cloned/deleted part of setup page, these drop downs created dynamically php , mysql

the dropdowns , buttons seem have correct names , incremented numbers on ends line up. not sure how how pass names javascript file below. i'd appreciate help, let me know if need else.

index:

$add_button        = '#btnadd[]'; $delete_button     = '#btndel[]'; $cloned_input      = '.clonedinput[]'; 

clonemultiple.php

<script type='text/javascript'>//<![cdata[  $(document).ready(function() {      var inputs = 1;       $('<?php echo $add_button; ?>').click(function() {         $('<?php echo $delete_button; ?>:disabled').removeattr('disabled');         var c = $('<?php echo $cloned_input; ?>:first').clone(true);         $('<?php echo $cloned_input; ?>:last').after(c);     });      $('<?php echo $delete_button; ?>').click(function() {         if (confirm('continue delete?')) {             --inputs;             $(this).closest('<?php echo $cloned_input; ?>').remove();             $('<?php echo $delete_button; ?>').attr('disabled',($('<?php echo $cloned_input; ?>').length  < 2));         }     });   }); //]]>   </script> 

here sample of html output requested

<div style="float:left; width:33%;"><hr/>     <input type="button" id="btnadd2" class="add" style="width:180px;" value="add air hammer" />     <div class="clonedinput2" id="clonedinputed2">air hammer         <label class="input input-right-small">             <input type="button" class="btndel delete" id="btndel2" value="x" disabled="disabled" />                 <select id="airhammer2" name="airhammer2" style="width:160px;">                     <option value="none" selected style="color:gray">none</option>                 </select>         </label>     </div> </div>  <div style="float:left; width:33%;"><hr/>     <input type="button" id="btnadd3" class="add" style="width:180px;" value="add boiler" />     <div class="clonedinput3" id="clonedinputed3">boiler         <label class="input input-right-small">             <input type="button" class="btndel delete" id="btndel3" value="x" disabled="disabled"/>                 <select id="boiler3" name="boiler3" style="width:160px;">                     <option value="none" selected style="color:gray">none</option>                 </select>         </label>     </div> </div> 

php

<?php      $getequipment = "select * rbs_equipment_type name != 'truck'";         $q_equipment = $conn->prepare($getequipment);             $q_equipment->execute();                 while($rowequipment = $q_equipment->fetch())                     {                     echo'                     <div style="float:left; width:33%;"><hr/>                         <input type="button" id="btnadd'.$rowequipment['id'].'" class="add" style="width:180px;" value="add '.$rowequipment['name'].'" />                             <div class="clonedinput'.$rowequipment['id'].'" id="clonedinputed'.$rowequipment['id'].'">'.$rowequipment['name'].'                                 <label class="input input-right-small">                                     <input type="button" class="btndel delete" id="btndel'.$rowequipment['id'].'" value="x" disabled="disabled" style="width:20px; margin:0px;padding-left:1px;padding-right:1px;padding-top:7px;padding-bottom:7px;"/>                                         <select id="'.$rowequipment['name'].''.$rowequipment['id'].'" name="'.$rowequipment['name'].''.$rowequipment['id'].'" style="width:160px;">                                         <option value="none" selected style="color:gray">none</option>';                                         /*find children*/      $getchildren = "select * rbs_equipment_model model_type_id = ".$rowequipment['id'];         $qchildren = $conn->prepare($getchildren);             $qchildren -> execute();                 while($rowchildren = $qchildren -> fetch())                     {                         echo "<option value='".$rowchildren['model_id']."'>".$rowchildren['model_name']."</option>";                     } echo                    '</select>                     </label>                 </div>            </div>';         } ?> 

demo: http://jsfiddle.net/1ry9h1e3/1/

no ids required, need add .wrapper class first div in loop

<div style="float:left; width:33%;" class="wrapper"> 

then jquery can use find elements needs find.

$(document).ready(function() {     var inputs = 1;       $('.add').click(function() {         var $wrapper = $(this).closest('.wrapper');         $wrapper.find('.delete').removeattr('disabled');         $wrapper.find('.clonedinput:first')             .clone(true)                 .appendto($wrapper)         ;     });      $('.delete').click(function() {         if (confirm('continue delete?')) {             --inputs;             var $wrapper = $(this).closest('.wrapper');             $wrapper.find('.delete:first').attr('disabled', $wrapper.find('.delete').length < 2);             $(this).closest('.clonedinput').remove();         }     }); }); 

Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

jsf - How to ajax update an item in the footer of a PrimeFaces dataTable? -

django - CSRF verification failed. Request aborted. CSRF cookie not set -