The DateTimePicker does not properly update the value field when onChange or onSelect is triggered

Currently, I am attempting to change the value of an input field whenever I select a different date. Below is the HTML code for the input.

<input type="text" name="completed_date" id="completed_date" size="12" class="completed_date fielddate" value="<?php echo date("d/m/Y",strtotime("now"))?>" readonly />

Here is the jQuery onSelect function that I am using:

 $(document).ready(function() {
  $('.completed_date').datetimepicker({
   timepicker: false,
   format: 'd/m/Y',
    onSelect: function() {
     var date = $(this).val();
     $('#completed_date').val(date);
    }
  });
 });

When I implement this code, it doesn't have any effect on my input field. However, if I replace the last line with:

$('#completed_date').text(date);

Then, it appends the selected date at the end of the HTML line.

<input type="text" name="completed_date" id="completed_date" size="12" class="completed_date fielddate" value="<?php echo date("d/m/Y",strtotime("now"))?>" readonly />
The new date gets added here.

By default, the value in the input field displays today's date but should be updated upon selection.

I have reviewed the documentation multiple times, but I cannot figure out what I might be overlooking. I would greatly appreciate any assistance provided.

While the visual date in the field updates correctly, the actual value within the input does not change. Therefore, when I click save, it always sends today's date instead of the one I selected.

Answer №1

When using bootstrap-datepicker, you can utilize the .datepicker method to load the date picker.

$('.completed_date').datepicker({
    timepicker:false,
    format:'d/m/yy'      
  })
.on("changeDate", function(e) {
 
  $('#completed_date').attr('value', e.format());
});
<script src="http://ajax.googleapis.com/ajax/libs/jquery/2.2.2/jquery.min.js"></script>
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<script src="http://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.6.4/js/bootstrap-datepicker.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.6.4/css/bootstrap-datepicker.css" rel="stylesheet"/>

<input type="text" name="completed_date" id="completed_date" size="12" class="completed_date fielddate" value="12/12/2016" />

Answer №2

Thank you so much for your assistance!

I have successfully modified your code and made it work with the DateTimePicker. Below is the updated code that I used. Once again, I really appreciate your help.

 $(document).ready(function() {
  $('.completed_date').datetimepicker({
    timepicker:false,
    format:'d/m/Y',
 });
 $('.completed_date').change(function() {
    var date = $(this).val();
    $('.completed_date').attr('value', date);
 });

});

This modification will now update the value field of an input connected to the DateTimePicker.

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

Instead of modifying the selected class, jQuery generates inline styles

Utilizing the following code to dynamically create a class: $("head").append('<style type="text/css"></style>'); var newStyleElement = $("head").children(':last'); newStyleElement.html('.move{transform: translateX(1 ...

Preventing default action in jQuery to avoid interference with serialization and following AJAX request

I am encountering an issue with a php page that contains multiple forms, where I need jQuery to hide each form after the submit button is clicked, without refreshing the entire page. The unique challenge here is that the id's of these forms are dynami ...

Conceal the <div> element if the <span>

Is there a way to hide the content within this div if the prop-value is empty? I specifically want to hide the text "First class" when Prop-value does not have any value. Any solutions? <div> <span class='prop-name'>F ...

Tips on using Ajax to post in HTML

Here is the code I have been working on: <script type="text/javascript"> var xmlDoc; var xmlhttp; function loadRates() { xmlhttp = new XMLHttpRequest(); xmlhttp.onreadystatechange = readRates; xmlhttp.open("GE ...

If PHP does not return data in a JSON encoded format, Ajax will not function properly

I have a PHP script that returns an array if an error occurs, otherwise it returns a <DIV> if(!empty($error)) { $true = true; $res = array('info' => '$error', 'error' => '$true'); echo json_enc ...

Using jQuery to first conceal the container before displaying the element within it

I need assistance with hiding a container element that has the class page_wrapper. After hiding it, I want to ensure that only one element within that container is displayed; this element has the id infinite_math_container. This is what I have tried so fa ...

Improvement in Select2 Change Event: Update the subsequent select2 box options based on the value change in the preceding select2 box

I need assistance with two select boxes, namely Category and Sub-category. My objective is to dynamically alter the available options in the subcategory box based upon the value selected in the category box. Additionally, I would like to load data for the ...

When using form.serialize() in Django forms, an empty object is being sent

Upon clicking the button, my AJAX request is sending an empty object Object { } instead of form data. The form on my page consists of checkboxes and its HTML structure is as follows: <form method="post" action="" data-id="filter-form"> //Included ...

JavaScript - Modify input character prior to appending it to the text area

I am working on creating a virtual keyboard using jQuery. Whenever I press 'a' in the textarea, I want it to display 'z' instead. In my investigation of typing a letter in the textarea, I discovered the following sequence: keyDown ev ...

Having trouble parsing PHP JSON encoded data in JavaScript

When I make an AJAX call to a PHP script that returns a JSON encoded object, I encounter some issues. $.post("php/getCamera.php", { cam_id: identifier }, function(data){ console.log(data); //var camera = JSON.parse( ...

The selected attribute does not function properly with the <option> tag in Angular

Currently, I am faced with a challenge involving dropdowns and select2. My task is to edit a product, which includes selecting a category that corresponds to the product. However, when I open the modal, the selected group/category is not displayed in the d ...

What is the correct way to incorporate scrollIntoView() using jQuery?

I'm trying to implement a jQuery function that will enable the last reply to scroll into view. This is my current code: function displayLastReply(replies){ replies.each(function() { if($(this).index() < nIniRep || afterReply){ $( ...

design facebook type box scroll bar

After extensively searching through various stackoverflow answers in an attempt to change the CSS for a Facebook likebox scrollbar, I have had no success. Below is the code that I am currently using: <div id="fb-root"></div> <script>(fun ...

Button to access overlay menu on my map with OpenLayers 3

I am trying to add a menu button on top of my map that, when clicked, will display a window. I have created the map div and the overlayMenu button div, but unfortunately, the button is not showing up where I want it to. I would like the button to be locate ...

Resolving a Tricky Challenge with jQuery's

I am facing an issue with a function that animates images through crossfading. This function is responsible for rotating banners on a website. By calling the function as animateSlideshow("play"), it starts animating and sets up a timeout. On the other hand ...

Trigger an alert message upon clicking a button within CK Editor

Is it possible to trigger an alert message using ckeditor when a specific button is clicked? Below is the code snippet for reference: $(document).ready(function () { $(".cke_button__bold").click(function () { editor.on("CKEDITOR.cke_butto ...

Make the minimum height of one div equal to the height of another div

My query is somewhat similar to this discussion: Implementing a dynamic min-height on a div using JavaScript but with a slight twist. I am working on a dropdown menu within a WordPress site, integrated with RoyalSlider. The height of the slider div adjust ...

I'm looking to develop a straightforward panorama application for Windows Phone 7 utilizing PhoneGap/Cordova – any tips on where to start

After searching extensively, I couldn't find any examples of PhoneGap/Cordova-based HTML5 apps for Windows Phone 7 that demonstrate how to create a panorama or pivot style app. These are key features of the OS UI that I want to incorporate into my app ...

JavaScript Processing Multiple Input Values to Produce an Output

Hello, I am working on creating a stamp script that will allow users to enter their name and address into three fields. Later, these fields will be displayed in the stamp edition. Currently, I have set up 3 input fields where users can input their data. He ...

If the AJAX call takes too long to return, it may not return any data

I am managing a large database with over 1000 entries, and the data size is expected to grow further. To display a subset of this data in a table, I have implemented a search feature using AJAX calls to load data dynamically based on user input. Here is ...