Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Reinitiate jquery datepicker after append the date field

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 2.67k
    Comment on it

    Hello Dear Readers,

    Most of times, when we append the text field of datepicker using jQuery it not work, in other words when we integrate the dynamic datepicker using jQuery append method then datepicker work only with first field.

    In this post we will discuss about the same problem of jQuery.

    When we integrate the datepicker field using datepicker library in the first time it has to load in the DOM, but after that when we append the next datepicker field using jQuery it doesn't initiate in the DOM.

     

    <!DOCTYPE html>
    <html>
    <head>
    	<title>jQuery Testing</title>
    	<script type="text/javascript" src="http://code.jquery.com/jquery-1.11.1.js"></script>
    	<link rel="stylesheet" href="http://code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
    	<script src="http://code.jquery.com/jquery-1.10.2.js"></script>
    	<script src="http://code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
    	<script type="text/javascript">
    		$(document).ready(function() {
    			$(".addSpan").click(function() {
    				var i = $("#dateTab input") .length + 1;
    				$("#dateTab").append($("<label>Date</label><input class='getDate id_"+ i +"' type='text' name='name[]' />"));
    			});
    		
    			$( ".getDate" ).datepicker();
    		});		
    
    	</script>
    </head>
    <body>
    
    <div id="dateTab">
    	<label>Date</label><input type="text" class="getDate id_1" name="name[]" />
    </div>
    <span class="addSpan">Add</span>
    
    </body>
    </html>

     

    In above code datepicker will work only for first field, because datepicker library not initiate for appended fields.

     


    So, this is a simple idea that if we reinitiate datepicker function with each appended datepicker fields, then it will work.


    Below code for date picker will work for each append fields because in this code we have reinitiated the datepicker library function for each fields.

    <!DOCTYPE html>
    <html>
    <head>
    	<title>jQuery Testing</title>
    	<script type="text/javascript" src="http://code.jquery.com/jquery-1.11.1.js"></script>
    	<link rel="stylesheet" href="http://code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
    	<script src="http://code.jquery.com/jquery-1.10.2.js"></script>
    	<script src="http://code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
    	<script type="text/javascript">
    		$(document).ready(function() {
    			$(".addSpan").click(function() {
    				var i = $("#dateTab input") .length + 1;
    				$("#dateTab").append($("<label>Date</label><input class='getDate id_"+ i +"' type='text' name='name[]' />").datepicker()); //reinitiate the datepicker
    			});
    		
    			$( ".getDate" ).datepicker();
    		});	
    
    	</script>
    </head>
    <body>
    
    <div id="dateTab">
    	<label>Date</label><input type="text" class="getDate id_1" name="name[]" />
    </div>
    <span class="addSpan">Add</span>
    
    </body>
    </html>
    

     

    In above code we add datepicker function with each appended field which initiate in the DOM for each datepicker field.

     


    Thanks,
    Vipul Srivastava

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: