Showing the Datepicker upon clicking the button

I am trying to implement a datepicker that shows up when a button is clicked. Unfortunately, my attempts have been unsuccessful so far. I am working with Materialize Css and have experimented with using an input type of text, but it's not quite what I'm looking for.

<div class="container">
     <div class="row center">
       <i>Date</i>
       <button class="btn waves-effect waves-light datepicker" type="submit" name="action">PICK DATE</button>
     </div>
   </div>

Here is the jQuery code I've attempted:

$('.datepicker').pickadate({
    selectMonths: true, 
    selectYears: 15 
  });

Answer №1

If you want to display the datepicker when a button is clicked, you must use the show option.

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>jDatapicker JQuery</title>
  <script src="//code.jquery.com/jquery-1.10.2.js"></script>
    <link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
  <script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
  <script>
  $(function() {
    $( "#datepicker" ).datepicker();
  });
    function show_dp(){
      $( "#datepicker" ).datepicker('show'); //Display on button click
     }
  </script>
</head>
<body>
 
<p>Date: <input type="text" id="datepicker"></p>
  <button onclick="show_dp();">Show Datepicker</button> 
 
 
</body>
</html>

Another way to do this without an input field:

<!doctype html>
<html lang="en>

<head>
  <meta charset="utf-8>
  <title>jDatapicker JQuery>
  <script src="//code.jquery.com/jquery-1.10.2.js></script>
  <link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css>
  <script src="//code.jquery.com/ui/1.11.4/jquery-ui.js></script>
  <script>

    function toggle_dp() {
      dp = $("#datepicker");
      if (dp.attr('datepicker')) {
        dp.datepicker('destroy');
        dp.removeAttr('datepicker');
      } else {
        dp.datepicker();
        dp.attr('datepicker', 1);
      }


    }
  </script>
</head>

<body>

  <div id="datepicker"></div>
  <button id="button" onclick="toggle_dp();">Toggle Datepicker</button>


</body>

</html>

Answer №2

In my opinion, a more effective approach would be to display the picker only when the button is clicked, rather than when the input field is clicked. This way, users have the flexibility to manually modify the input as needed.

One strategy I use involves utilizing a hidden input to link to the picker:

<div class="input-field datepicker-prefix">
    <i class="material-icons prefix">perm_contact_calendar</i>
    <input type="hidden" class="datepicker" />
    <input />
</div>

Then, I set up a click event to reveal the picker:

$('.datepicker-prefix .prefix').click(function () {
    $(this).parent().find('.datepicker').datepicker('open');
});

You can explore my complete solution for both date and time pickers here

Answer №3

Feel free to utilize this code that I have crafted:

<!doctype html>
<html lang="en>
<head>
  <meta charset="utf-8>
  <title>jDatapicker JQuery</title>
  <script src="//code.jquery.com/jquery-1.10.2.js></script>
    <link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css>
  <script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
  <script>
  $(function() {
    $( "#datepicker" ).datepicker();
  });
  </script>
</head>
<body>

<p>Date: <input type="text" id="datepicker"></p>


</body>
</html>

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

Error: The Tabs component is expecting a different `value`. The Tab with the current `value` ("0") is not present in the document structure

I am encountering an issue while using MUI tabs. The error message I receive is as follows: MUI: The value assigned to the Tabs component is not valid. The Tab with this value ("0") does not exist in the document layout. Please ensure that the tab item is ...

CKEditor directive in AngularJS does not properly enforce the maxlength attribute in textarea

I am currently working on an AngularJS application with the CKEditor plugin. I have created a directive for CKEditor and everything seems to be functioning properly. However, I am facing an issue where I need to limit the character length to 50. I tried us ...

After being added to the flow in Node-RED, my customized node is no longer functional

While attempting to create a custom node in Node-RED, I encountered an issue where the node becomes "stuck" and unusable when dropped into the flow. The error displayed in the console (F12) is as follows: Uncaught TypeError: Cannot read property 'hasO ...

Show Zeroes in Front of Input Numbers

I am working with two input fields that represent hours and minutes separately. <input type="number" min="0" max="24" step="1" value="00" class="hours"> <input type="number" min="0" max="0.60" step="0.01" value="00" class="minutes"> This se ...

Executing javascript href using Python in Selenium

Currently, I am attempting to use Selenium in Python to click on a href JavaScript link. The HTML code appears as follows: HTML Example and my goal is to click on javascript:goType(1). This is the approach I have taken: advance_search = browser.find_el ...

Customizing hyperlink styles with JavaScript on click

Hey there! I'm experimenting with something new. I've managed to change the background color of each link after it's clicked, but now I'm facing a challenge: How can I revert the original style when another link is clicked? Here's ...

IE10 is not effectively handling multiple media queries

Within my CSS file, I've included a series of media queries specifically tailored for ie10... @media screen and (max-width :1510px), screen\0{ div.jw-menu-3col-sm li a .menuItem { padding: 0 12px; } } @media screen ...

Adding content to the parent element immediately after it is generated using jQuery

Is there a way to trigger $(e).append() as soon as the element e is created without using setTimeout()? I find that method inefficient. Are there any DOM events that can detect changes in a subtree of a DOM element? Usually, I would just add the code to t ...

Is it possible to use vanilla JavaScript scroll event with AngularJS framework?

I am attempting to track the window offset from the top of the document, but I am facing issues with jQuery scroll functionality. Can a vanilla JavaScript scroll event listener be used effectively within an Angular environment? app.directive('owlCaro ...

Tips for accessing the value from an input field in an AngularJS application

Looking at my DOM structure below, <input class="k-textbox ng-pristine ng-untouched ng-valid ng-valid-required" type="text" placeholder="" ng-blur="controller.isDirty=true" ng-change="controller.isDirty=true" ng-model="controller.model.value" ng-requir ...

Can you tell me which BBC channel is airing this animation?

Is anyone familiar with the animation on the login screen of BBC when entering the day, month, and year? Check it out here: ...

What steps should I take to make my Vue JS delete function operational?

As I work on developing my website, I've encountered some challenges. Being new to coding, I'm struggling with creating a functional delete user button. When I click delete, it redirects me to the delete URL but doesn't remove the entry from ...

What is the best way to use regular expressions to match text with varied endings?

Here is my current situation. <h2>Details</h2>\n +<p>(.*)<br />|</p> ^ there's a tab space, wondering if there's a better way to show one or more (seems to be working) I am attempting to f ...

Update destination upon click

I'm new to JavaScript and attempting to modify a redirect link using checkboxes that will modify the URL, followed by a button that will use the updated URL. However, I'm facing some challenges with my code and could use some guidance. Here is t ...

Unable to make a POST request using axios

I'm having trouble populating a table using vue.js and axios in my visual studio project. Every time I run the solution, I see an empty table with just the heading Title. I experimented with another POST request method but unfortunately, it didn&apos ...

Having trouble with redundant code while updating state in ReactJS - React JS

Currently, I am working on a prayer times web app using reactJS (nextjs). To achieve this, I first fetch the geolocation coordinates and then retrieve the city and country name based on these coordinates. Following that, I obtain the prayer times for the s ...

Using CSS to ensure that the cards have consistent heights when using both React and Bootstrap

I am currently working with Bootstrap 4 in React. I have encountered an issue where the card for Item 1 is shorter than that of Item 2, and I would like both cards to have the same height. Additionally, I anticipate adding more cards at the bottom in the ...

The validation of forms using ajax and jsp is not functioning

I've been working on developing a username validation system using JSP and AJAX, but I keep encountering an error that looks like this: POST ://localhost:8080/web_application/deneme.jsp Error Message: 405 (Method Not Allowed) This is my JSP page, c ...

Transferring the input value to a different input field with Keyup in Angular 9

Is there a way to instantly pass data from one input field to another in Angular? I am facing some issues with this. How can I troubleshoot this problem? Component.ts export class AppComponent { number = ''; //initialized the text variable ...

Tips for creating a button hover effect with dynamic color and opacity

On my webpage, the button color is determined by an admin using JavaScript on page load. Since I don't know what color will be used, I can't apply rgba(). While using opacity does change the button's color, it also affects the text, which is ...