Code in my js file
$(document).ready(function() {
// Check if #donationtype1 has a value
var donationType1Value = $('#donationtype1').val();
var startdate1 = $('#start-date1').val();
// If #donationtype1 has a value, set the corresponding option as selected in #donationtype
if (donationType1Value) {
// console.log(startdate1);
$('#donationtype').val(donationType1Value).change();
var formattedStartDate = moment(startdate1, 'YYYY-MM-DD').format('MM/DD/YYYY');
// Set the formatted date as the value of the #start-date input field
$('#start-date').val(formattedStartDate).change();
}
});
for above code value is set but not displayed in input for start-date
@shweta11 Oops!
Verify the date format:
Ensure that the startdate1 variable holds a valid date in the format 'YYYY-MM-DD'. If the date format is incorrect or contains invalid characters, the moment() function might not parse it correctly.
// Check if #donationtype1 has a value
$(document).ready(function() {
var donationType1Value = $('#donationtype1').val();
var startdate1 = $('#start-date1').val();
// If #donationtype1 has a value, set the corresponding option as selected in #donationtype
if (donationType1Value) {
// console.log(startdate1);
$('#donationtype').val(donationType1Value);
var formattedStartDate = moment(startdate1, 'YYYY-MM-DD').format('MM/DD/YYYY');
// Set the formatted date as the value of the #start-date input field
$('#start-date').val(formattedStartDate);
}
});
The .val() method in jQuery is used to get or set the value of an input element:
$('input').val(); // get the value of the input element
$('input').val('new value'); // set the value of the input element to 'new value'
The .change() method in jQuery is used to bind an event handler to the change event of an input element:
$('input').change(function() {
// do something when the input element changes
});