how to add field in form in yii1 with ajax? - javascript

I am using date picker widget in my YII1 form. I also have a button to add more dates. When the page is first time loaded the date picker widget works. When I click on add more button, I added the new filed to the from but at that time date picker does not work.
How can I add datepicker widget fields on ajax to a form?
or there is any other way doing?

In Ajax Success re-initialize datepicker
$( "#datepicker" ).datepicker();
your problem will solve.better if you post your code here

I think your issue is because of 'Id'
You just change the datepicker displaying attribute from 'Id' to 'Class',
Ie,the newely added fields id and already added fields id,s are same so date picker wont work with multiple with same id,so you should change the datepicker displaying attribute from 'ID' to 'Class'
then, it will work fine

Related

I don't want datetimepicker setting default value on click

I have a text input to which I have applied a DateTimePicker plugin
But when I click on the input before opening the DateTimePicker, it sets default value as current date, I want the value to stay empty as long as the user selects it from the DateTimePicker.
$('.datepicker').datetimepicker({
format: 'DD/MM/YYYY',
});
The DateTimePicker Library which i am using is:
eonasdan.github.io/bootstrap-datetimepicker
I have mentioned the answer in the comments above but just posting it as an Answer for someone who might face the same issue in the future
Initialize the DateTimePicker with useCurrent as false as shown below:
$('.datepicker').datetimepicker({format: 'DD/MM/YYYY',useCurrent:false});

jQuery Datepicker .val() is blank however a date is displayed?

This is my first question.
I have looked for an answer but my searches seem to bring back a lot of ways to enforce the datepicker value is empty, or being able to access the selected value which is not an option here.
Background to the problem
I have a table with a dynamic amount of rows, each row contains a few datepickers (we'll call them A datepicker, B datepicker, C datepicker). It also contains a child row that is hidden until the 'Expand for more details' button is clicked, at which point there are a few more fields appearing. One of those fields is a 'Comments' field.
The requirement I have is that when the A datepicker is changed that a 'Comment' MUST be required to go along with that. I am able to tell when the datepicker is changed and therefore to send a message via the validator of the 'Comments' telling the user that it is now required.
The requirement
When entering details into the comments section, I need to be able to check if there is a date selected in the A datepicker so that we can dynamically warn the user that the comments section needs to be X many characters IF there is a date in the A datepicker.
The problem
It appears that when the jQuery datepicker is changed that the .value is not updated. When the row is populated it is possible that the A datepicker can be empty, not all fields require that particular date to be set and therefore not all 'Comment' fields need to be set either.
So I can tell when the datepicker is changed and get the 'Comments' field to be required. However a user can then fill out the 'Comments' field with a blank entry or enter a value that doesn't meet the X amount of characters limit. This behaviour is OK for when there is not a date selected. So when a date is selected I need to enforce the rule.
My problem is that I can't tell if a date is selected or not!
In the example below I had a blank datepicker, I then chose a date and opened developer tools. In the overview of the web page we can see that the date is set to 20th Jan 2018.
Overview of the web page
however through the developer tools we can see that the value is actually empty.
Datepicker value is set to ""
This behaviour is also true when the datepicker was not initially blank, rather than the value being the new date it still retains the old date. In the example below I changed the date from the 23rd of December 2017 to the 31st of December 2017.
Pre-Populated datepicker
And in developer tools the value is still set to the previous value.
Changed to new date
Technicalities
I am using MVC and jQuery, I am not looking for a solution that involves having to go back to the controller as that usually requires the user having to press a submit button and my requirement is that it needs to be able to respond upon selecting the date and entering comments. I'd just like a way to be able to get the displayed date from the datepicker and not it's previous value.
This matters because if it's going from previously having a date into now a blank date, it affects the requirement so that comments are no longer required as there isn't a date. Vice versa if going from a blank date to a selected date.
Things I have tried
I am trying to write this into the OnChange method of 'Comments', so that I can check if there is a date selected or not.
I have tried directly calling it in my .js file with the below. For option 4 and 5 it's contained in a .each loop that goes through ALL the rows in my table and finds the row relating to the 'Comments' field that was changed.
(Please note that parentRow refers to the selected table row which contains the dates (the row that contains the comments field is the child row)).
1) var dateValue = $(parentRow).find("input[name$='datepicker_A']").datepicker("getDate");
2) var dateValue = $(parentRow).find("input[name$='datepicker_A']").val();
3) var datepicker_A = document.getElementById('datepicker_A');
var dateValue = datepicker_A.val();
4) var dateValue = $.datepicker.parseDate("d/M/y", $(this).find("input[name$='datepicker_A']").val());
5) var dateValue = $(this).find("input[name$='datepicker_A']").val();
All of the above return the previous value of the datepicker and not the newly selected one. So my question is.. how do I get it to recognise the new value?
TL:DR
jQuery datepicker doesn't seem to change the .val() property of the input field when selecting a new date, instead the .val() is always the previous value. How can I access the new value via javascript/jQuery?
Your code should look something like this:
<div>
Select date: <input id="thedate" type="text" class="date-pick" />
</div>
And your JavaScript (I am using jQuery 1.9.1 with jQuery UI 1.9.2 here too)
$(function() {
$('.date-pick').datepicker({
dateFormat: 'dd-mm-yy',
onSelect: function(dateText, inst) {
alert(dateText); // Put your code in here
}
});
});
Here's a JsFiddle

DatePicker date is wrongly binding to textbox-1 ? fiddle provided

I am dynamcically generating rows with some controls . In one of those controls i am having a textbox which is binded with datepicker .
Everything is working fine but there is a minor issue i.e when i select date from textbox-2 which is dynamically generated the selected date is apperaring in textbox-1 and textbox-2 is empty sadly .. so this goes on like if i have 10 textboxes in any textbox i select date end of the day i get the selected date in textbox-1 only ..
You can find the excat senario here : http://jsfiddle.net/JL26Z/8/
Correct me if i made any silly error
Regards
there is minor issue with the element, you created element with id so due to same id its causing problem.
please use class instead of id, it will work fine.
I have updated the fiddle, see here:
http://jsfiddle.net/JL26Z/10/
If you have a look at generated html when you add new fields by pressing button you will see that every text box has the same id. When you set date in calendar it searches for text box with matching id and finds first text box on your form and sets date for it. Make sure that you generate unique id for newly added controls. You can have a counter variable and increment it and append to id when you add new set of controls.
In your fiddle calendar shows for phone number field when it should probably show for date field.

Ways to combine jquery-ui Datepicker with inserting text on click.

I have a table list with many input fields that has the jqueury datpicker in it. Every input field has as class datepicker and a random id number. when clicked in the input field teh datpicker pops up. With a click on he date the dtae gets inserted. So the he datepicker works fine. But now i want to add an image or text link next to the input field, and when i click on the image/text the content on the input field changes into the selected text. But untill now i cannot combine datepicker and this fucntion into one....
example i tried:
<input type="text" id="a11" class="datepicker" name="sterilizationdate_1" value="<?php echo $sterilizationdate_1?>">
CSA
and the javascript
$('a.CSA').click(function(){
$('.datepicker').val($('.datepicker').val()+$(this).html());
});
I tested this code without the datepicker and it just works fine... why cant i combine the two?
FOUND ANOTHER SOLUTION
custom text insert in datepicker
You are trying to get the val() of input tag but calling val on anchor tag
$('a.CSA').click(function(){
$('.datepicker').val($('.datepicker').prev().val()+$(this).html());
});
Maybe you have another problem on how you call the Datepicker.
You can try:
$('a.CSA').on('click', function(){
$('.datepicker').val($('.datepicker').prev().val()+$(this).html());
});
Instead of:
$('a.CSA').click(function(){
$('.datepicker').val($('.datepicker').prev().val()+$(this).html());
});
Please this link .on()

How to show the selected date in datepicker?

I'm Working with Asp .net MVC3.i'm having a text box with a datepicker image that textbox having a date filled in it.when i click select the datepicker image the popped up datepicker has to be pointing out the date which is in textbox instead of pointing the current date.Is there any jquery for this?
Use the setDate option :
$('element').datepicker({
defaultDate: '02/02/12'
}).datepicker('setDate', '02/02/12');
FIDDLE
Works just fine by default.
See fiddle: http://jsfiddle.net/ymLe2/1/
$('#dated').datepicker({showOn: "button"});
You need to post your code, so as to be able to identify the cause of your problem.

Categories

Resources