I lost the jqery datepicker function in this script ...
1 Initially, I had a normal view that worked fine, having this code to enter the date ...
<input type="text" name="date_start" id="date_start" value="@(Model.TimeStart)" /> <script language="javascript" type="text/javascript"> $("#date_start").datepicker(); </script>
2- Then I converted this view to a partial view, which is dynamically loaded with ...
$('#TargetEmptyDiv').load("/MyController/MyAction");
3- Partial viewing loads normally, data can be entered (manually) and everything is in order. The only problem is that the jquery datepicker no longer works. I tried to assign it after boot using this ...
$('#TargetEmptyDiv').load("/MyController/MyAction", function () { $("#date_start").datepicker(); });
This did not work.
4 Later, I learned that jquery provides live () and delegate () functions that can attach javascript code to loaded elements "now and in the future." So, I tried this in document initialization ...
<script type="text/javascript"> $(document).ready(function () { $("#date_start").live("click", function () { $(this).datepicker(); }); }); </script>
and then also ...
<script type="text/javascript"> $(document).ready(function () { $("#date_start").delegate("click", function () { $(this).datepicker(); }); }); </script>
none of them worked, possibly because they directly refer to the “click” event, but the date-picker is “attached” (if this is the correct term) to the entire element.
So the question is: How to get jquery datepicker to work with elements loaded after dynamic loading?
Thank you for your time!
SOLVE: The problem was that at the bottom of _Layout.cshtml is this line ...
@Scripts.Render("~/bundles/jquery")
.. which loads a mini version of jQuery libraries (already mentioned by me). Thus, the libraries were loaded twice and became invalid, creating the error "datepicker () is not a function".
Just commenting or deleting this line, everything works fine!