When i add new datepicker dynamically the previous datepicker values reset? - javascript

I have form which contain Add more field button ,which dynamically adds new datepicker's whenever i click Add More field button.
The problem is ,Currently date field in the datepicker display the current date.
But when i add new Datepicker by clicking on the Add more Field Button , it adds one more Datepicker but this time it resets the previous value of Datepicker which i had set manually.
Below is my code:
html
<div id="container">
<input class="datepick" type="text"></input><button class="add">Add</button>
<input
</div>
javascript
$(".datepick").datepicker();
$(".datepick").datepicker().datepicker("setDate", new Date());
$(document).on('click', '.add', function() {
var appendTxt = '<input class="datepick" type="text" /><button class="add">Add</button>';
$("#container").append(appendTxt);
$(".datepick").datepicker();
$(".datepick`enter code here`").datepicker().datepicker("setDate", new Date());
});

Because of this line:
$(".datepick").datepicker();
All inputs which have the class .datepick will reinitialize. You should do something like this:
$(".datepick").last().datepicker();
or
$(".datepick:last").datepicker();
after you add the new input.datepick. This was the simple solution.
If you have any other .datepick's in another container after this container, this won't work too. You should be very specific about the appended datepicker element.
You should do this :
var appendTxt = '<input class="datepick" type="text" /><button class="add">Add</button>';
var newInput = $(appendTxt).appendTo("#container");
newInput.datepicker();
newInput.datepicker().datepicker("setDate", new Date());

Related

How to format hidden input field in ftl/html

I have to two fields type text in html, and two datepicker for those fields.
Now, when I choose the dates, they automatically populate text fields but in hidden input field those values are stored with the time also.
Like this:
value="2022-04-13T00:00:00+02:00"
Is there a way to format that input to be like this:
value="2022-04-13"
Thanks in advance...
What browser are you using?
in this code example everything works as you expected.
<input id="date" type="date" />
<input id="hidden" type="hidden" />
<script>
const date = document.querySelector("#date");
const hidden = document.querySelector("#hidden");
date.addEventListener("input", (e) => {
hidden.value = date.value;
});
</script>

Insert time into a field with a button

how do i insert a date time into a field (military time) on the click of a button.
I found this, similar code, but I was unable to make it work:
$(function() {
$('#time').click(function() {
var time = new Date();
$('#time-holder').val(time.toDateString());
});
});
<input type="text" value="" id="time-holder">
<input type="button" value="time" name="timer" id="time">
Why does it not show the date when i click the button?
This will insert the time instead of the date:
$(
function(){
$('#time').click(function(){
var time = new Date();
$('#time-holder').val(time.toTimeString());
});
}
);
Use time.toString() to get both the date and the time.
More info at https://www.w3schools.com/jsref/jsref_totimestring.asp
Update:
JSFiddle: https://jsfiddle.net/cuva5y4z/

How to fill data automatically in a textfield using the info provided in the <select> tab using javascript

How to fill data automatically in a textfield using the info provided in the tab using javascript. And also the filled data cannot be edited.
The textfield may fill data based on calculation on the option selected in tab.
Please help.
I don't so much understand your question but try this:
HTML
<input type="text" id="t" disabled />
JS
var text = document.getElementById('t');
text.value = 658.57; // your calc result
If you use jQuery, you can do something like this:
$(document).ready(function() {
var VALUE_OF_SELECTED_TAB = this.value;
// HERE DO YOUR CALCULATIONS
$(YOUR_SELECT_ELEMENT).on('change', function() {
$(YOUR_TEXTFIELD_ELEMENT).html(
'<input type="text" value="'+ YOUR_CALCULATION_RESULT
+'" class="field left" readonly="readonly" >');
});
});

Assigning Datepicker to javascript variable

I have some very simple js working where a user enters info in a textbox, and the input is repopulated in another area of the document with a click function. Some of the input fields require dates, and I would like them to appear as date pickers. I have tried to set the datepicker as a style in the head of the doc as instructed in this guide http://javarevisited.blogspot.ca/2013/10/how-to-use-multiple-jquery-ui-date.html but it isn't working.
Is this because the variable Id overrides the class ID? Can I assign the datepicker function in the script?
<script type = "text/javascript">
function sayHi(){
var BPCName = document.getElementById("BPCName");
var OUTBPC = document.getElementById("OUTBPC");
var name = BPCName.value;
OUTBPC.value = " " +name+ ""
} // end sayHi
<style> .datepicker {}</style>
<script>
$(function() {
$( "#datepicker" ).datepicker();
});
</script>
<form action = "">
<fieldset>
<label>Start Date: </label>
<input type = "text"
class=datepicker
id = "BPCName" />
<h1>TEST</h1>
<form action = "">
<fieldset>
<label>Start Date: </label>
<input type = "text"
class=datepicker
id = "BPCName" />
you just need to call Jquery and JqueryUI in your headers then use this
$("input.datepicker").datepicker();
//this code will set all your inputs with "datepicker" class, to appear the datepicker
//from JqueryUI
and you should put
class="datepicker"
not simply
class=datepicker

Javascript - loop through datepickers and set date

I don't know Javascript at all. I have a form where there can be any number of datepicker textboxes. When the user selects a date in the first datepicker textbox, then I want all the remaining datepicker textboxes to have that same date.
Does this require a function?
Edit: I tried to create a function, but I don't know javascript at all!
function UpdateValuationDates(event) {
$valuationDatePicker = $(event.target);
var valuationDate = $valuationDatePicker.datepicker("getDate");
if (valuationDate != null) {
//loop through all items
document.getElementById("dateValuationDate").Text
$valuationDatePicker.datepicker("setDate", valuationDate);
$valuationDatePicker.trigger('change');
}
}
So I think this can be ignored. I have also read that there is a datepicker on selected event:
$(".date").datepicker({
onSelect: function(dateText) {
display("Selected date: " + dateText + "; input's current value: " + this.value);
}
});
So I guess I need to edit this code to populate the rest of the textboxes, but how to find out at runtime how many there are?
The HMTL has a repeater with the datepicker repeated x number of times:
<abc:DatePicker runat="server" ID="dateValuationDate"
With the help of html's input type=date and some basic classes' knowledge, you can do that.. Considering you have following Date pickers:
<input type="date" class="dateTime">
<input type="date" class="dateTime">
<input type="date" class="dateTime">
Now you simply need to listen to a change in any one of there values:
$(".dateTime").on("change", function(){
and when the change occurs, get the changed value and set all other date pickers to that new value:
$(".dateTime").val($(this).val());
So it'll be something like this:
$(document).ready(function(){
$(".dateTime").on("change", function(){
$(".dateTime").val($(this).val());
});
});
See the DEMO here
EDIT: Considering you're new to JavaScript, here's how i'm getting the reference to all those elements, through .className, as they all have same class name so for each event (change, update value) they all will be referenced.

Categories

Resources