javascript - Get value from select and clone form number of times -


i have select box , want user select value , after getting value forms added in page according selected value , if user selects "1" 1 form added , if user select "10" 10 forms. if nothing given or not selected no form display . seems problem in js , add forms add other more forms dont expect.

<div class="form-group">                                         <label>number of travellers</label>                                         <select class="form-control" id="travellersnumber">                                             <option value="0">select numbers of travellers</option>                                             <option value="1">1</option>                                             <option value="2">2</option>                                             <option value="3">3</option>                                             <option value="4">4</option>                                             <option value="5">5</option>                                             <option value="6">6</option>                                             <option value="7">7</option>                                             <option value="8">8</option>                                             <option value="9">9</option>                                             <option value="10">10</option>                                         </select>                                     </div>  <div class=bookform>  <div class=col-xs-12>  <h3 class=bookform-heading>submit details</h3>  </div>  <div class="col-xs-12 col-md-3">  <div class=form-group>  <label>first name</label>  <input class=form-control name=firstname>  </div> </div> </div> 

.bookform{     display:none;   } 

function bookformtoggle(){     var traveller = $("#travellersnumber");     var form = $(".bookform");     var heading = $('.bookform-heading');     function unhideform(){         var travellervalue = parseint(traveller.val());         for(i=0;i<travellervalue;i++){                 heading = heading.html('traveller '+ (i+1) +' information');                 form.clone().insertafter(form);         }     }     traveller.on("change",unhideform);     unhideform(); } bookformtoggle(); 

hope want.

$(function () {  	$("#travellersnumber").on('change', function () {  		var traveller = $(this);  		var form = $("#original");  		var heading = $('.bookform-heading');  		var travellervalue = parseint(traveller.val());  		$("#original").hide();    		$("[id*='original_']").not("#original").remove(); //remove forms except original form    		for (i = 0; < travellervalue; i++) {  			if (i == 0) {  				heading = heading.html('traveller 1 information');  				$("#original").show();  			}  			else  			{  				var clonediv = form.clone().prop("id", "original_" + i).appendto("#travellersdetail");  				clonediv.find(".bookform-heading").html('traveller ' + (i + 1) + ' information');  			}  		     		}  	});  });
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>    <style>  	.bookform {  		display: none;  	}  </style>    <div class="form-group">  	<label>number of travellers</label>  	<select class="form-control" id="travellersnumber">  		<option value="0">select numbers of travellers</option>  		<option value="1">1</option>  		<option value="2">2</option>  		<option value="3">3</option>  		<option value="4">4</option>  		<option value="5">5</option>  		<option value="6">6</option>  		<option value="7">7</option>  		<option value="8">8</option>  		<option value="9">9</option>  		<option value="10">10</option>  	</select>  </div>    <div class="bookform" id="original">  	<div class=col-xs-12>  		<h3 class="bookform-heading">submit details</h3>  	</div>  	<div class="col-xs-12 col-md-3">  		<div class=form-group>  			<label>first name</label>  			<input class=form-control name=firstname>  		</div>  	</div>  </div>    <div id="travellersdetail"></div>


Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -