javascript - jQuery replace for the new select box only and not the earlier one -


i explain scenario. have website , trying collect "type of business" members in website. display existing businesses have been entered , allow user choose 1 of them. if user wishes enter "type of business", have allowed him choose, "**other". when clicks on "**other" box displayed in user enters "type of business" , gets added.

now if user has multiple business, allow him "add other". when clicks on "add other" again shown "type of business" including 1 entered earlier. if user chooses "**other" again - box displayed , when user enters "type of business" same displayed in earlier box , current box.

i want new value should replaced new select box , not earlier one. how make happen.

i have writed small php code similar.

<!doctype html> <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script> <script> function showfield(name){   if(name=='**other')  var oth_busi_type = prompt("please enter new county", "");     if (oth_busi_type != null) {                 $(var_1).append('<option selected="selected" value="'+oth_busi_type+'">'+oth_busi_type+'</option>');     }}  $(document).on('click', '.add-more', function() {             console.log($('.details:eq(0)').clone());                $('.details:eq(0)').clone(true).appendto('.details-parent'); }); </script> </head> <body> <div class="details-parent">     <div class="details">     <table class="extra_table" summary=""> <tr>     <td class="aligntop bold">*location:</td>     <td class="extra_field_input">          <select class='county' name='county'  id='var_1' onchange='showfield(this.options[this.selectedindex].value)' >                            <option value="select county..." selected="selected">select county...</option>               <option value="dodge" >dodge</option>               <option value="douglas" >douglas</option>               <option value="lancaster" >lancaster</option>               <option value="madison" >madison</option>               <option value="**other">**other</option>          </select>     </td> </tr> <tr>         <td class="aligntop bold extra" style="display:none;">**enter if **other</td>     <td class="extra_field_input">           <input style="display:none;" type="text" size="17" name="other_county" id="busi_type"  />     </td> </tr> </table> <div class="add-more">     <a href="#">add more</a>  </div>  </div>     </div>      </body> </html> 

please find updated code below

i created

<!doctype html> <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script> <script> $(document).ready (function (e){     var details = $('.details:eq(0)').clone(true);     $(document).on ('change', '.county', function (e){         if ($(this).val() =='**other'){             var oth_busi_type = prompt("please enter new county", "");         }          if (oth_busi_type != null) {             $(this).append('<option selected="selected" value="'+oth_busi_type+'">'+oth_busi_type+'</option>');         }     });      $(document).on('click', '.add-more', function() {         console.log($('.details:eq(0)').clone());         $(details).clone(true).appendto('.details-parent');     }); }); </script> </head> <body> <div class="details-parent">     <div class="details">         <table class="extra_table" summary="">             <tr>                 <td class="aligntop bold">*location:</td>                 <td class="extra_field_input">                      <select class='county' name='county'  id='var_1' >                           <option value="select county..." selected="selected">select county...</option>                           <option value="dodge" >dodge</option>                           <option value="douglas" >douglas</option>                           <option value="lancaster" >lancaster</option>                           <option value="madison" >madison</option>                           <option value="**other">**other</option>                      </select>                 </td>             </tr>             <tr>                 <td class="aligntop bold extra" style="display:none;">**enter if **other</td>                 <td class="extra_field_input">                       <input style="display:none;" type="text" size="17" name="other_county" id="busi_type"  />                 </td>             </tr>         </table>         <div class="add-more">             <a href="#">add more</a>         </div>     </div> </div> </body> </html> 

Comments

Popular posts from this blog

amazon web services - S3 Pre-signed POST validate file type? -

c# - Check Keyboard Input Winforms -