Display a series of numbers in a stylish Bootstrap button with uniform dimensions

I am trying to find a way to show the number of days in a specific month within a bootstrap button. However, the code I have been using does not evenly distribute the buttons in terms of height and width.

I would like the display to look similar to the screenshot provided, with a set number of buttons in a row that are evenly distributed. While it doesn't have to be exactly seven buttons, I want them to be spaced out evenly.

https://i.sstatic.net/3rnKj.png

const [day, month, year] = "18/03/2022".split("/");

const getAllDaysInMonth = (month, year) =>
  Array.from({
      length: new Date(year, month, 0).getDate()
    }, // get next month, zeroth's (previous) day
    (_, i) => new Date(year, month - 1, i + 1) // get current month (0 based index)
  );

const allDatesInOctober = getAllDaysInMonth(month, year);

const allDateinMonth = allDatesInOctober.map(x => x.toLocaleDateString([], {
  day: "numeric"
}));

const list = document.getElementById('year-of-day');

window.onload = () => {
  list.innerHTML = allDateinMonth.map(i => `<button type="button" class="btn btn-info m-2">${i}</button>`).join('');
};
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet"/>
<div class="row rowSpacing">
  <div class="col-md-12">
    <div class="form-group col-md-12">
      <label for="inputEmail4">Select Dates</label>
      <div class="input-group" id="year-of-day">
        <button type="button" class="btn btn-info m-2"> </button>
      </div>
    </div>
  </div>
</div>

Can someone suggest a solution to address this issue?

Answer №1

The alignment issue arises from varying button widths due to the different digit counts in the numbers. To address this, establish a consistent base width for all buttons. Consider the example below:

const [day, month, year] = "18/03/2022".split("/");

const getAllDaysInMonth = (month, year) =>
  Array.from({
      length: new Date(year, month, 0).getDate()
    }, // get next month, zeroth's (previous) day
    (_, i) => new Date(year, month - 1, i + 1) // get current month (0 based index)
  );

const allDatesInOctober = getAllDaysInMonth(month, year);

const allDateinMonth = allDatesInOctober.map(x => x.toLocaleDateString([], {
  day: "numeric"
}));

const list = document.getElementById('year-of-day');

window.onload = () => {
  list.innerHTML = allDateinMonth.map(i => `<button type="button" class="btn btn-info m-2" style="width: 50px">${i}</button>`).join('');
};
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet"/>
<div class="row rowSpacing">
  <div class="col-md-12">
    <div class="form-group col-md-12">
      <label for="inputEmail4">Select Dates</label>
      <div class="input-group" id="year-of-day">
        <button type="button" class="btn btn-info m-2"> </button>
      </div>
    </div>
  </div>
</div>

The only inclusion made in the code above is style="width: 50px".

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Ensuring the screen reader shifts focus to the previous element

Utilizing the screen reader to redirect focus back to the previous element has proven to be a challenge for me. After clicking the Return button, it will vanish and the Submit button will take its place. If the Submit button is then clicked, it disappears ...

Dynamic styling with jQuery input focus animation

Whenever I interact with the input field, I want my border colors to animate in and fade out smoothly. But I'm facing some challenges. Here is how I've set it up: HTML <input type="text" id="search-input" placeholder=&quo ...

HTML: A peculiar table setup with an image displayed on the right side and text aligned to the left side. Strangely, the

Just starting out and seeking assistance <table style="border:none" cellpadding="0" cellspacing="0"> <tbody> <tr> <td style="border:none" valign="top" vertical-align:"top"; width="20%"> <div><img class= ...

Router failure resulted in an internal server error

When navigating to a page in my router, I make a REST API request to retrieve data from the server in the beforeEnter clause as shown below: beforeEnter: (to, form, next) => { getData().then( (response) => { ...

Guide for invoking a servlet via a navigation bar hyperlink and implementing a jQuery feature for smooth scrolling upon clicking

Is there a way to call a servlet from a navigation bar href link and at the same time trigger a jQuery function for smooth scrolling down? I attempted to call the servlet using an onclick href link, it successfully calls the servlet but does not trigger t ...

When removing the class "img-responsive" from an image, the bootstrap columns begin to overlap

Just starting out with Bootstrap while working on an Angular2 project and I have a question. Currently, I have a map-component taking up 3 columns on the left-hand side, but every time I resize the browser, the image also resizes. I want the image to rema ...

Enhance the angular 2 dependencies within the angular2-cli project

After experimenting with Angular 2 and following the guide on their website, I attempted to switch to Angular 2 CLI. However, the Angular 2 CLI project does not have the latest dependencies, resulting in errors from the compiler related to certain commands ...

Populate a string with HTML elements

I need help loading a string into UIwebview. The text is plain coming from the database. I want the webview to display the text in bold. How can I add an HTML <b> tag or any other tag to format this simple text sourced from the database? ...

There was an issue compiling the file static/css/main.b6d8a2cb.css with the Css Minimizer plugin. An error occurred: TypeError - Unable to modify properties of undefined (setting 'value')

PS C:\Users\Hp\Desktop\web technology\NCT\carrental> npm run build [email protected] build react-scripts build Attempting to generate an optimized production build... Compilation failed. The error message "static/c ...

React not correctly returning multiple values in function: TypeError: cannot iterate over undefined (cannot read property Symbol(Symbol.iterator))

Although there are numerous questions with a similar title, none of them address my specific concern. My current project involves a React app that retrieves data from a backend built with Node.js and MySQL. However, I encountered the following error while ...

What are your thoughts on using inline PHP to assign values to JavaScript variables?

Imagine you have a PHP document and need to determine if the request was made with or without query parameters using JavaScript. Since there is no built-in function to extract values from the URL, you may need to resort to some sort of workaround. While it ...

Having trouble with d3 / svg layering when introducing new nodes overtime

Struggling with a frustrating issue on my d3 force directed map project. Initially, I render the necessary nodes and links, then periodically check for updates through AJAX. The problem arises when adding new nodes and links – they appear over existing c ...

When I view the website on a tablet in landscape mode, the navigation bar vanishes

I just finished creating a responsive menu for a website. Here's how it works: when viewing the site on a regular browser with a width less than 768px, the navigation menu items are stacked and hidden. To reveal them, the user needs to click on a tog ...

Is it feasible to have multiple versions of React coexisting in a monorepo?

I have a monorepo set up with npm workspaces: ├─ lib │ └─ Foo └─ src ├─ App └─ Web I am looking to upgrade the Web package to React 18 while keeping App on React 17 My current dependencies are as follows: ├─ lib │ └ ...

How can I horizontally center an element in CSS/HTML if auto-margin does not produce the desired result?

This is a snippet of the CSS code I'm using for my menu: ul { text-align: left; display: inline; margin: auto; width: 50%; position: fixed; top: 0; list-style: none; } I've attempted to use auto-margin left and right ...

Steps for recreating a Jquery Mobile form element with the same name after destroying it

As I delve into Jquery Mobile, I encounter a scenario where I must dynamically generate form fields (select, input, etc) using the following approach: $fieldInput = $('<input type="text" name="' + fieldName + '" />'); Initially, ...

Find the two numbers within a specific range in an array using jQuery

I have two arrays and I need to check for any duplicate ranges. How can I achieve this? let startingArray = ['1', '6.1', '10', '31','6.2',3]; let endingArray = ['2', '9.9', '30&ap ...

useEffect does not trigger a rerender on the primary parent component

I am facing an issue where the main parent component does not re-render when I change the state 'click button' in another component while using useEffect. Oddly enough, the main <App /> component only re-renders properly when I reload the p ...

What is the best way to determine which section of a promise chain is responsible for an error in Javascript?

(Please excuse any errors in my English) I am currently studying JavaScript promises. Below is a simple JavaScript code snippet for node.js (using node.js version v10.0.0) that asynchronously reads and parses a JSON file using promise chaining. const fs ...

Refreshing ng-repeat dynamically with $http without reloading the page

Just a quick heads-up, I'm new to AngularJS In my AngularJS application, I am using the $http service to fetch data. Each row has an update button that needs to trigger a server-side method (the API is already returning data) to sync with a third-par ...