jQuery Datepicker, display 2 differently datepickers on same page - javascript

I am using a the jQuery [Datepicker][1] in a website. I am having trouble displaying 2 datepickers on the same page in different ways.
One which will only display once you click the text box, and once you select your date it disappears.
Second I would like to display inline, but I cannot control one with out the other messing up.
I need help finding a work around so I can use 2 datepickers in a different way on the same page. I tried giving the second a separate id but that didn't work.
My first Date picker code:
<script> <!-- Datepicker -->
$(function() {
$( ".datepicker" ).datepicker();
});
</script>
<input type="text" class="search-input datepicker" placeholder="Check-In">
<input type="text" class="search-input datepicker" placeholder="Check-Out">
Here is my second Date picker code:
<script>
$(function() {
$( "#datepicker2" ).datepicker({
numberOfMonths: 3,
showButtonPanel: true
});
});
</script>
<p><input type="text" id="datepicker2"></p>
Thank you

Simply give each text box a unique ID, then use that to create the datepicker. You can then put whichever options you want inside each datepicker() call:
$(function()
{
$( "#datepicker1" ).datepicker();
$( "#datepicker2" ).datepicker();
});
HTML:
<p><input type="text" id="datepicker1"></p>
<div id="datepicker2"></div>
jsfiddle

I've done it before, my solution:
<script> <!-- Datepicker -->
$(function()
{
$( "#datepicker1" ).datepicker();
$( "#datepicker2" ).datepicker();
});
</script>
<input type="text" class="search-input datepicker" placeholder="Check-In">
<input type="text" class="search-input datepicker2" placeholder="Check-Out">
Maybe there's any better solution?

Related

cancel jQuery datepicker default click on input

I would like to ask if there is any way to cancel the default behaviour of the jquery datepicker, that so when I click the input field where the datepicker is attached, my custom logic to flow and the datepicker calendar to not be shown.
I read the documentation and used the suggested "hide" option but it seems to not work flawless.
Here is a js fiddle that I used:
jquery datepicker jsfiddle
function hideIt(){
$( "#datepicker" ).datepicker( "hide" );
//custom logic
}
function showIt(){
$( "#datepicker" ).datepicker("show");
}
No need to add button. Just give it to handle datepicker.
$(function() {
$("#datepicker").datepicker({
constrainInput: true,
showOn: 'button',
buttonText: 'Show It !'
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/themes/base/jquery-ui.css"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/jquery-ui.js
"></script>
<p>Date:
<input type="text" id="datepicker" /> </p>
Note : To prevent to write on text box make it disable if you want.
I have found a solution to the problem by using "fictional" button with imageOnly property.
Here is the jsfiddle: jsfiddle
Javascript:
$(function() {
$("#datepicker").datepicker({
showOn: 'button',
buttonImage: 'randomlocation/nonexisting-calendar-img.gif',
buttonImageOnly: true,
buttonText: ''
})
});
function hideIt(){
//custom logic
}
function showIt(){
$( "#datepicker" ).datepicker("show");
}
Html:
<p>Date: <input type="text" id="datepicker" onclick="javascript:hideIt();" /></p>
<input type="button" onclick="javascript:showIt();" value="Show It !" />
Try like this
function hideIt(){
}
function showIt(){
$( "#datepicker" ).datepicker();
$('#datepicker').focus();
$('#datepicker').prop('disabled', true);
}

Block jQuery UI calendar dates when radio selected

I am trying to add a bit of functionality to a jQuery UI calendar but am fairly new to jQuery and am having a bit of problem with my code. This is what I have at the moment:
JS Fiddle code
<input name="button" type="radio" value="3days"/><label>Add 3 Days</label>
<p><input name="button" type="radio" value="0days"/></p>
<p>Date: <input type="text" id="datepicker" /></p>
Jquery:
$(function(){
$('input[value="3days"]').click(function(){
if ($(this).is(':checked'))
{
$('#datepicker').datepicker({ minDate: +3 });
}
else
{
$('#datepicker').datepicker({ minDate: 0 });
}
});
});
I basically want to block the first 3 days if a radio button is selected. If its not selected then I would like only the current date onward to be selectable.
If anyone could suggest some alterations to my code that would be great.
See my updates
$(function(){
$('input[type=radio]').click(function(){
$('#datepicker').datepicker("destroy");
if ($(this).val() == "3days") {
$('#datepicker').datepicker({ minDate: +3 });
} else {
$('#datepicker').datepicker({ minDate: 0 });
}
});
});
jsfiddle

Use jquery timepicker and datepicker simultaneously

I need to use both timepicker and datepicker. But only one of them works. My code is mentioned below. I am using multiple jquery files.
<script type="text/javascript">
$(document).ready(function() {
$(".datepicker").datepicker({
changeMonth: true,
changeYear: true
});
});
</script>
<script type="text/javascript">
$(document).ready(function() {
$('.timepicker').timepicker({
showPeriod: true,
showLeadingZero: true
});
});
</script>
Date : <input type="text" class="datepicker"/>
Time : <input type="text" class="timepicker"/>
Check Timepicker with Datepicker This one plugin has both pickers as a single component.
I used https://github.com/trentrichardson/jQuery-Timepicker-Addon
<link href="yourpath/jquery-ui-timepicker-addon.css'?>" rel="stylesheet" media="all">
<script src="yourpath/jquery-ui-timepicker-addon.js'?>"></script>
$(function() {
$( ".datepicker" ).datepicker();
});
$(function() {
$(".timepicker").timepicker();
});
and html
input type="text" name="date" value="" placeholder="Conference Name" id="datepicker"
input type="text" name="start_time" value="" placeholder="Starting Time" class="timepicker"
input type="text" name="end_time" value="" placeholder="Ending Time" class="timepicker"
it works for me.I think its jquery version problem
Make sure the library is loaded from CDN http://cdnjs.com/libraries/jquery-ui-timepicker-addon
Once the js and CSS are loaded successfully, you can use the plugin as per examples given in httpp://jonthornton.github.io/jquery-timepicker/. I used the following options to get AM/PM selector;
$('input.timepicker').timepicker({controlType: 'select',
timeFormat: 'hh:mm TT',
dropdown: true,
scrollbar: false,
ampm: true});
For detailed options list refer to httpp://timepicker.co/options/
2 links have httpp instead of http cz SO wouldn't allow me to post many links in one answer.

Open two jquery datepickers simultaneously

I have two input fields containing a class that triggers a datepicker to open. Both work fine independently, i.e when I click on one or the other but I want for both datepickers to open when either input field is clicked.
Here is the script part of the code
$(document).ready(function() {
$( ".datefields" ).datepicker({ dateFormat: 'dd/mm/yy',numberOfMonths: 1, yearRange: "2012:2014", changeYear: true, changeMonth: true});
and this is the html
<div id="quoteformcollection">
<h1>Collection</h1>
<input type"text" class="startTbl locationfields" id="AutoLocStart" value="Please Type a Collection Location"onclick="clearVal(this);"/>
<input type="hidden" id="DepotStart" value=""/></td>
<input type="text" class="datefields" id="collectiondate" value="21/05/2012"/>
<input type"text" class="timefields" value="12:00" />
</div>
<div id="quoteformreturn">
<h1>Return</h1>
<input type"text" class="locationfields" value="Enter the city or location for return" />
<input type"text" id="returndate" class="datefields" value="21/05/2012" />
<input type"text" class="timefields" value="12:00" />
</div>
I have tried looking for an answer myself but am quite new to jquery and struggling a bit so any help would be much appreciated.
I would also like for whatever value is selected in the first datepicker, for the default date in the second to be incremented by x number of days, which again I am not sure of the best way to go about it.
Any advice would be hugely appreciated!
Try this code for instance to link the two datapickers:
$('#collectiondate').datepicker({
dateFormat: "#",
onSelect: function(dateText, inst) {
var dateText = eval(dateText);
var min = new Date();
min.setTime(dateText);
$('#end').datepicker('option', 'minDate', min);
}
});
$('#returndate').datepicker({
dateFormat: "#",
});
I think you'd have to use the calendar 'inline' and manage how it's popped up yourself through jQuery / Javascript. You need to point the cal to a div tag to use it inline.
http://jqueryui.com/demos/datepicker/#inline

Jquery date picker - want display date in an input field

Hava an jquery calendar:
$(function() {
$("#datepicker").datepicker({
minDate: 'today',
maxDate: "+90D",
showOn: "button",
buttonImage: "images/calendar.gif",
buttonImageOnly: true,
dateFormat: "D, dd MM, yy"
});
});
and
<form method="post">
<div id="datepicker">
<input type="text" id="datepicker" name="datepicker"/>
</div>
</form>
I want to display the date inside the input field...Please help me
The problem is that you have duplicated your id value datepicker. It's there twice.
IDs are supposed to be unique (one per page). Even if you were using classes, you'd be calling datepicker() on both the <div> and the <input>, which is surely not what you want.
Just remove the id from the div.
Working demo: http://jsfiddle.net/wesley_murch/PMFwg/
you cannot have two same id in a single page , use class for the same instead of ids
<form method="post">
<div id="datepicker1">
<input type="text" id="datepicker" name="datepicker"/>
</div>
</form>
above will work fine as div id you can change to datepicker1 or custom it

Categories