Johnine Johnine - 2 months ago 24
jQuery Question

bootstrap datepicker not working inside of a javascript

Why does the bootstrap not working inside a javascript but working outside .

Here's my code:



var date = new Date();
date.setDate(date.getDate());

$('.datepicker').datepicker({
startDate: date,
autoclose: true,
format: "mm-dd-yyyy",
todayHighlight: true,
orientation: "top auto",
todayBtn: true,
todayHighlight: true,
});
var rowCount = 1;

function removeRow(removeNum) {
jQuery('#rowCount'+removeNum).remove();
rowCount --;
rcount();
}

function addMoreRows(frm) {
rowCount ++;
var recRow = '<p id="rowCount'+rowCount+'"><tr><td><input id="datepkr" class="form-control datepicker" style="width:60%; float:left;" name="" type="text"/></td><td><a style="float:left;" href="javascript:void(0);" onclick="removeRow('+rowCount+');"><i class="glyphicon glyphicon-remove" style="color:red;"></i></a></td></tr></p>';
jQuery('#addedRows').append(recRow);

if (rowCount>15){
$('#btnAdd').prop('disabled', true);
}

}
function rcount(){
if (rowCount==3){
jQuery('#x2').hide('slow');
jQuery('#x3').show('slow');
}
if (rowCount==4){
jQuery('#x3').hide('slow');
jQuery('#x4').show('slow');
}
if (rowCount==5){
jQuery('#x4').hide('slow');
jQuery('#x5').show('slow');
}
if (rowCount==6){
jQuery('#x5').hide('slow');
jQuery('#x6').show('slow');
}
if (rowCount==7){
jQuery('#x6').hide('slow');
jQuery('#x7').show('slow');
}
if (rowCount==8){
jQuery('#x7').hide('slow');
jQuery('#x8').show('slow');
}
if (rowCount==9){
jQuery('#x8').hide('slow');
jQuery('#x9').show('slow');
}
if (rowCount==10){
jQuery('#x9').hide('slow');
jQuery('#x10').show('slow');
}
if (rowCount==11){
jQuery('#x10').hide('slow');
jQuery('#x11').show('slow');
}
if (rowCount==12){
jQuery('#x11').hide('slow');
jQuery('#x12').show('slow');
}
if (rowCount==13){
jQuery('#x12').hide('slow');
jQuery('#x13').show('slow');
}
if (rowCount==14){
jQuery('#x13').hide('slow');
jQuery('#x14').show('slow');
}
if (rowCount==15){
jQuery('#x14').hide('slow');
jQuery('#x15').show('slow');
}
if (rowCount==16){
jQuery('#x15').hide('slow');
jQuery('#x16').show('slow');
}

if (rowCount>14){
$('#btnAdd').prop('disabled', true);
}
else{
$('#btnAdd').prop('disabled', false);
}
}

<script src="https://code.jquery.com/jquery-3.1.0.js" integrity="sha256-slogkvB1K3VOkzAI8QITxV3VzpOnkeNVsKvtkYLMjfk=" crossorigin="anonymous"></script>

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap-theme.min.css">
<script src="http://code.jquery.com/jquery-1.11.1.js"></script>
<script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/js/bootstrap.min.js"></script>
<script type="text/javascript" src="js/blogController.js"></script>

<table>
<tr id="rowId">
<td><input name="" type="text" value="" class="datepicker"/></td>
<td><button type="button" id="btnAdd" onclick="addMoreRows()" class="btn btn-primary"><i class="glyphicon glyphicon-plus"></i>ADD</button></td>
</table>
<div id="addedRows"></div>
</td>
</tr>





Here it is Showing the Datepicker
but in a javascript generated input its not showing

Hope someone will help . Thanks .

Answer

You need to correct your JS code. Please try below code.

$(document).ready(function() {
    bindDatePicker($('.datepicker'));

  $('button#btnAdd').click(function() {
    var recRow = '<tr><td><input class="form-control datepicker" style="width:60%; float:left;" name="" type="text"/></td></tr>';
    alert(recRow);
        jQuery('#addedRows').append(recRow);
        bindDatePicker($('.datepicker'));
        if (rowCount>15){
            $('#btnAdd').prop('disabled', true);
        }
  });
});

function bindDatePicker(element) {
        $(element).datepicker({         
            autoclose: true,
            format: "mm-dd-yyyy",
            todayHighlight: true,
            orientation: "top auto",
            todayBtn: true,
            todayHighlight: true,  
        });
}

You can check here for demo : JSFIDDLE

Comments