Bootstrap 5 Datepicker with Vanilla JS

Bootstrap 5 Datepicker with Vanilla JS
Code Snippet:Datepicker - using Vanilla JS
Author: mike foskett
Published: January 11, 2024
Last Updated: January 22, 2024
Downloads: 30,381
License: MIT
Edit Code online: View on CodePen
Read More

This lightweight vanilla JS code snippet helps you to create datepicker for Bootstrap 5 projects. It comes with easily navigatable days, months, and years with a custom date format. You can set dd/mm/yyyy or any other suitable date format according to your needs. Similarly, you can define the custom titles and CSS styles for datepicker.

How to Create Datepicker for Bootstrap 5

1. First of all, load the Bootstrap 5 framework and datepicker’s CSS into the head tag of your webpage.

<!-- Bootstrap 5 CSS -->
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.0/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-KyZXEAg3QhqLMpG8r+8fhAXLRk2vvoC2f3B09zVXn8CA5QIVfZOJ3BCsw2P0p/We" crossorigin="anonymous">

<!-- Vanilla Datepicker CSS -->
<link rel='stylesheet' href='https://cdn.jsdelivr.net/npm/vanillajs-datepicker@1.1.4/dist/css/datepicker.min.css'>   

2. After that, create the input tag with a class name "datepicker_input" and wrap this input into the div element and define its class name “input-group”.

<div class="input-group mb-4">
  <i class="bi bi-calendar-date input-group-text"></i>
  <input type="text" class="datepicker_input form-control" placeholder="Date input 3 placeholder" required aria-label="Date input 3 (using aria-label)">
</div>

3. You can also create a floating label datepicker by creating the HTML structure as follows:

<div class="form-floating input-group mb-4">
  <i class="bi bi-calendar-date input-group-text"></i>
  <input type="text" id="datepicker2" class="datepicker_input form-control" placeholder="DD/MM/YYYY" required>
  <label for="datepicker2">Date input label 2</label>
</div>

4. Now, load the Bootstrap and datepicker JavaScript file before closing the body tag and done.

<!-- Bootstrap 5 JavaScript Bundle with Popper -->
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.0/dist/js/bootstrap.bundle.min.js" integrity="sha384-U1DAWAznBHeqEIlVSCgzq+c9gqGAJn5c/t99JyeKa9xxaYpSvHU5awsuZVVFIhvj" crossorigin="anonymous"></script>

<!-- Vanilla Datepicker JS -->
<script src='https://cdn.jsdelivr.net/npm/vanillajs-datepicker@1.1.4/dist/js/datepicker-full.min.js'></script>

5. Finally, initialize the datepicker function as follows:

/* Bootstrap 5 JS included */
/* vanillajs-datepicker 1.1.4 JS included */

const getDatePickerTitle = elem => {
  // From the label or the aria-label
  const label = elem.nextElementSibling;
  let titleText = '';
  if (label && label.tagName === 'LABEL') {
    titleText = label.textContent;
  } else {
    titleText = elem.getAttribute('aria-label') || '';
  }
  return titleText;
}

const elems = document.querySelectorAll('.datepicker_input');
for (const elem of elems) {
  const datepicker = new Datepicker(elem, {
    'format': 'dd/mm/yyyy', // UK format
    title: getDatePickerTitle(elem)
  });
}     

That’s all! I hope you have successfully integrated this vanilla JS date picker into your Bootstrap 5 project. If you have any questions or suggestions, let me know by comment below.

3 thoughts on “Bootstrap 5 Datepicker with Vanilla JS”

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

About CodeHim

Free Web Design Code & Scripts - CodeHim is one of the BEST developer websites that provide web designers and developers with a simple way to preview and download a variety of free code & scripts. All codes published on CodeHim are open source, distributed under OSD-compliant license which grants all the rights to use, study, change and share the software in modified and unmodified form. Before publishing, we test and review each code snippet to avoid errors, but we cannot warrant the full correctness of all content. All trademarks, trade names, logos, and icons are the property of their respective owners... find out more...

Please Rel0ad/PressF5 this page if you can't click the download/preview link

X