Datepicker does not work when content dynamically added

Hello, as title says datepicker does not work when content dynamically added

my page built like this:

include("header"); // with all needed JS and CSS

content where on every click is dynamically loaded

include("footer.php");

footer file holds:

    <script>
		$(document).ready(function() {

			
			$('.datepicker').datetimepicker({
				format: 'DD.MM.YYYY',
				locale: 'lv'
			});
			
		});
	</script>

right before </body>

As much as i got it from what i read is that DOM just doesnt know that new content is there!?
I also was not be able to find a solution :frowning:

Any help would be appreciated!
Thanks!

It does not work on dynamically created content because the (shown) setup only runs at page load. If you want to add the datepicker to dynamic content, you need to init the datepicker on that content as well.

@Dormilich i just tested this solution <?php include_once("../../datepicker.php"); ?> and it works, but it need to be included in every file. …
There is no better solutions?

when you create the dynamic element, also run the datepicker on it.

$('<input/>').appendTo(body).datepicker(options)
1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.