Bootstrap datepicker not working when put on page through a javascript function
<p>I have implemented Bootstrap datepicker in my webpage. I am using Bootstrap for CSS Styling.
Using bootstrap recommends that JQuery and Bootstrap JS should be included in the page at last. But doing so is making bootstrap datepicker malfunction.
Following is a snippet of my page through which i am able to use datepicker as well as bootstrap JS for a bootstrap Dropdown.</p>

<pre><code>&lt;head&gt;
    &lt;script src="../js/jquery.js"&gt;&lt;/script&gt;
    &lt;script src="../js/bootstrap.min.js"&gt;&lt;/script&gt;
    &lt;script type="text/javascript" src="http://cdnjs.cloudflare.com/ajax/libs/moment.js/2.0.0/moment.min.js"&gt;&lt;/script&gt;
    &lt;link href="https://netdna.bootstrapcdn.com/twitter-bootstrap/2.2.2/css/bootstrap-combined.min.css" rel="stylesheet"&gt;
    &lt;link rel="stylesheet" type="text/css" media="screen" href="https://tarruda.github.com/bootstrap-datetimepicker/assets/css/bootstrap-datetimepicker.min.css"&gt;
        &lt;link href="../css/bootstrap.css" rel="stylesheet"&gt; 
        &lt;script type="text/javascript" src="https://netdna.bootstrapcdn.com/twitter-bootstrap/2.2.2/js/bootstrap.min.js"&gt;&lt;/script&gt;
        &lt;script type="text/javascript"src="https://tarruda.github.com/bootstrap-datetimepicker/assets/js/bootstrap-datetimepicker.min.js"&gt;&lt;/script&gt;
    &lt;/head&gt;
    &lt;div class="btn-group pull-right"&gt;
                  &lt;button type="button" data-toggle="dropdown" class="btn btn-default dropdown-toggle" aria-haspopup="true" aria-expanded="false"&gt;Dropdown&amp;nbsp;&amp;nbsp;&lt;span class="glyphicon glyphicon-triangle-bottom"&gt;&lt;/span&gt;&lt;/button&gt;
                    &lt;ul class="dropdown-menu"&gt;
                        &lt;li&gt;&lt;a href="#"&gt;&lt;span class="glyphicon glyphicon-info-sign"&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp;About &lt;b&gt;1&lt;/b&gt;&lt;/a&gt;&lt;/li&gt;
                        &lt;li&gt;&lt;a data-toggle="modal" data-target="#optionModal"&gt;&lt;span class="glyphicon glyphicon-alert"&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp;Report a Bug&lt;/a&gt;&lt;/li&gt;
                        &lt;li&gt;&lt;a data-toggle="modal" data-target="#optionModal"&gt;&lt;span class="glyphicon glyphicon-text-background"&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp;Suggest Enhancement&lt;/a&gt;&lt;/li&gt;
                        &lt;li&gt;&lt;a data-toggle="modal" data-target="#optionModal"&gt;&lt;span class="glyphicon glyphicon-phone-alt"&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp;Contact Us&lt;/a&gt;&lt;/li&gt;
                    &lt;/ul&gt;
            &lt;/div&gt;
    &lt;div class="container"&gt;
        &lt;div id="questionblock"&gt;
            &lt;div class= "customscrollbar" id="questiondiv"&gt;
                &lt;div&gt;
                    &lt;button onclick="addtest();" class="btn btn-primary" style="margin-left:10px; margin-top:7px;"&gt;&lt;span class="glyphicon glyphicon-plus"&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp;Add New Test&lt;/button&gt;
                    &lt;a type="button" class="btn btn-default" style="margin-left:10px; margin-top:7px;"&gt;&lt;span class="glyphicon glyphicon-refresh"&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp;Refresh&lt;/a&gt;
                &lt;/div&gt;
            &lt;/div&gt;
        &lt;/div&gt;
    &lt;/div&gt;

&lt;div id="displaymasterblock"&gt;
        &lt;div class="alert alert-info fade in" id="addtestheader"&gt;
            &lt;span class="glyphicon glyphicon-plus"&gt;&lt;/span&gt;&amp;nbsp;Add New Test
        &lt;/div&gt;
        &lt;em&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;All fields are mandatory&lt;/em&gt;
            &lt;form&gt;
                &lt;div class="form-inline"&gt;
                    &lt;div id="formelements"&gt;
                        &lt;label&gt;1. Name of Test :&lt;/label&gt;
                        &lt;input type="text" class="form-control" id="testname" style="height:30px;" placeholder="Maximum 20 Characters"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
                        &lt;label&gt;2. Scheduled on :&lt;/label&gt;
                        &lt;div id="datetimepicker" class="input-append date"&gt;
                        &lt;input type="text" style="height:30px;" placeholder="Date &amp; Time of Test"&gt;&lt;/input&gt;
                        &lt;span class="add-on" style="height:30px;"&gt;
                        &lt;i data-time-icon="icon-time" data-date-icon="icon-calendar"&gt;&lt;/i&gt;
                        &lt;/span&gt;
                          &lt;/div&gt;
                        &lt;script type="text/javascript"&gt;   //datepicker
                          $("#datetimepicker").datetimepicker({
                            format: "dd/MM/yyyy hh:mm:ss",
                            todayHighlight: true,
                            pick12HourFormat: true,
                            maskInput: true,
                          });
                        &lt;/script&gt;
                    &lt;/div&gt;
                &lt;/div&gt;
                &lt;div class="alert alert-info fade in" id="addtestheader" style="height:25px;font-size:15px;padding:2px;"&gt;
                    &lt;span class="glyphicon glyphicon-user"&gt;&lt;/span&gt;&amp;nbsp;Select Target students for test
                &lt;/div&gt;
                &lt;div class="form-inline"&gt;
                    &lt;div id="formelements"&gt;

                          &lt;div class = "input-group"&gt;
                            &lt;span class = "input-group-addon" style="width:40px;"&gt;3. Programme&lt;/span&gt;
                            &lt;select id="programme" name="programme" class="form-control" style="width:150px;" onchange="changeinprog();"&gt;
                              &lt;option value="0" selected&gt;Select One&lt;/option&gt;
                            &lt;/select&gt;
                            &lt;span class = "input-group-addon"&gt;4. Branch&lt;/span&gt;
                            &lt;select id="branch" name="branch" class="form-control" onchange="changeofbranch();"&gt;
                              &lt;option value="0"&gt;Select Programme&lt;/option&gt;
                            &lt;/select&gt;
                        &lt;/div&gt;
                        &lt;div class = "input-group" style="margin-top:10px;"&gt;
                            &lt;span class = "input-group-addon" style="width:40px;"&gt;5. Semester&lt;/span&gt;
                            &lt;select id="semester" name="semester" class="form-control" style="width:150px;" onchange="changeinsemester();"&gt;
                              &lt;option value="0"&gt;Select Programme&lt;/option&gt;
                            &lt;/select&gt;
                            &lt;span class = "input-group-addon"&gt;6. Course&lt;/span&gt;
                            &lt;select id="course" name="course" class="form-control" style="width:270px;" &gt;
                              &lt;option value="0"&gt;Problem solving and programming skills&lt;/option&gt;
                            &lt;/select&gt;
                        &lt;/div&gt;
                    &lt;/div&gt;
                &lt;/div&gt;
                &lt;center&gt;
                &lt;div id="errordiv" style="width:400px;"&gt;&lt;/div&gt;
                &lt;button type="button" class="btn btn-success"&gt;Save&lt;/button&gt;
                &lt;button type="button" class="btn btn-default"&gt;Cancel&lt;/button&gt;

                    &lt;/center&gt;
                &lt;/form&gt;

&lt;script src="../js/bootstrap.min.js"&gt;&lt;/script&gt;
</code></pre>

<p>Markup inside <code>&lt;div id="displaymasterblock"&gt;</code> has been minified and used in a JS function to get triggered on a button click. Following is that JS function-</p>

<pre><code>function addtest()
{
    var element = '&lt;div class="alert alert-info fade in" id="addtestheader"&gt;&lt;span class="glyphicon glyphicon-plus"&gt;&lt;/span&gt;&amp;nbsp;Add New Test&lt;/div&gt;&lt;em&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;All fields are mandatory&lt;/em&gt;&lt;form&gt;&lt;div class="form-inline"&gt;&lt;div id="formelements"&gt;&lt;label&gt;1. Name of Test :&lt;/label&gt;&lt;input type="text" class="form-control" id="testname" style="height:30px;" placeholder="Maximum 20 Characters"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;label&gt;2. Scheduled on :&lt;/label&gt;&lt;div id="datetimepicker" class="input-append date"&gt;&lt;input type="text" style="height:30px;" placeholder="Date &amp; Time of Test"&gt;&lt;/input&gt;&lt;span class="add-on" style="height:30px;"&gt;&lt;i data-time-icon="icon-time" data-date-icon="icon-calendar"&gt;&lt;/i&gt;&lt;/span&gt;&lt;/div&gt;&lt;script type="text/javascript"&gt;$("#datetimepicker").datetimepicker({format: "dd/MM/yyyy hh:mm:ss",todayHighlight: true,pick12HourFormat: true});&lt;/script&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class="alert alert-info fade in" id="addtestheader" style="height:25px;font-size:15px;padding:2px;"&gt;&lt;span class="glyphicon glyphicon-user"&gt;&lt;/span&gt;&amp;nbsp;Select Target students for test&lt;/div&gt;&lt;div class="form-inline"&gt;&lt;div id="formelements"&gt;&lt;div class = "input-group"&gt;&lt;span class = "input-group-addon" style="width:40px;"&gt;3. Programme&lt;/span&gt;&lt;select id="programme" name="programme" class="form-control" style="width:150px;" onchange="changeinprog();"&gt;&lt;option value="0" selected&gt;Select One&lt;/option&gt;&lt;option value="1"&gt;B.Tech&lt;/option&gt;&lt;option value="2"&gt;M.Tech&lt;/option&gt;&lt;option value="3"&gt;MCA&lt;/option&gt;&lt;option value="4"&gt;M.Sc.&lt;/option&gt;&lt;/select&gt;&lt;span class = "input-group-addon"&gt;4. Branch&lt;/span&gt;&lt;select id="branch" name="branch" class="form-control" onchange="changeofbranch();"&gt;&lt;option value="0"&gt;Select Programme&lt;/option&gt;&lt;/select&gt;&lt;/div&gt;&lt;div class = "input-group" style="margin-top:10px;"&gt;&lt;span class = "input-group-addon" style="width:40px;"&gt;5. Semester&lt;/span&gt;&lt;select id="semester" name="semester" class="form-control" style="width:150px;" onchange="changeinsemester();"&gt;&lt;option value="0"&gt;Select Programme&lt;/option&gt;&lt;/select&gt;&lt;span class = "input-group-addon"&gt;6. Course&lt;/span&gt;&lt;select id="course" name="course" class="form-control" style="width:270px;" &gt;&lt;option value="0"&gt;Problem solving and programming skills&lt;/option&gt;&lt;/select&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;center&gt;&lt;div id="errordiv" style="width:400px;"&gt;&lt;/div&gt;&lt;button type="button" class="btn btn-success"&gt;Save&lt;/button&gt;&lt;button type="button" class="btn btn-default"&gt;Cancel&lt;/button&gt;&lt;/center&gt;&lt;/form&gt;';
    document.getElementById('displaymasterblock').innerHTML = element;
}
</code></pre>

<p>What aabove function does is it puts all markup inside the <code>&lt;div id="displaymasterblock"&gt;</code> 
Datepicker is working i.e. calendar and clock icons are showing up and functioning perfectly when it is directly included in the markup as shown above however when i am using button click to show the same markup the <strong>datepicker stops functioning</strong>.</p>

<p>Please help me out here. I believe there is something wrong with external resource inclusion order but not sure.
I hope i explained myself properly.</p>

<p><a href="https://i.stack.imgur.com/yNse8.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/yNse8.png" alt="Datepicker rendering and functioning properly"></a>
<a href="https://i.stack.imgur.com/JDrMF.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/JDrMF.png" alt="Datepicker malfunctioning when rendered on page through a button click"></a></p>