I'm creating a PHP site for property. I'm new to jQuery and jQuery UI but can't seem to find the answer anywhere else.
Please see this screen shot (full size):
For for every "received" and "expiry" box I want a jQuery datePicker box to appear and format as shown.
To test this I tried to create an example.
<link type="text/css" href="css/ui-lightness/jquery-ui-1.8.9.custom.css" rel="Stylesheet" />
<script type="text/javascript" src="js/jquery-1.4.4.min.js"></script>
<script type="text/javascript" src="js/jquery-ui-1.8.9.custom.min.js"></script>
<script>
$(function() {
$( "#datepicker" ).datepicker({ dateFormat: 'yy-mm-dd' });
$( "#datepicker2" ).datepicker({ dateFormat: 'yy-mm-dd' });
});
</script>
<style>
.ui-datepicker {
font-size: 80%;
}
</style>
<p>Date: <input id="datepicker" type="text" /></p>
<p>Date2: <input id="datepicker2" type="text" /></p>
Which works fine, but how do I get a date picker to come up for ANY textbox without having to repeat the JS so many times.
For say, 50 properties, there may be 200 input boxes on the page with a date needing to be filled in.
Here is some example code from the screen shot.
<tr>
<td>Property ID</td>
<td>House Number</td>
<td>Address Line 1</td>
<td>Gas Expiry</td>
<td>Gas Received</td>
<td>Electric Expiry</td>
<td>Electric Received</td>
<td>Property Active</td>
<td>Update</td>
</tr>
<tr>
<td>4</td>
<td>6</td>
<td>East Street</td>
<td><input name="gas_expiry[4]" type="text" id="gas_expiry[4]" value="2011-08-03" /></td>
<td><input name="gas_received[4]" type="text" id="gas_received[4]" value="" /></td>
<td><input name="electric_expiry[4]" type="text" id="electric_expiry[4]" value="" /></td>
<td><input name="electric_received[4]" type="text" id="electric_received[4]" value="" /></td>
<td>
<select name="active[4]" id="active[4]">
<option value="0" selected="selected">No</option>
<option value="1">Yes</option>
</select>
</td>
<td><input name="edit[]" type="checkbox" id="edit[]" value="4" /></td>
</tr>
Probably something really simple, I appreciate any help.
What I do is create a calendar CSS class and have the behavior attached to each member of that CSS class like so:
$( ".calendar" ).datepicker({ dateFormat: 'yy-mm-dd' });
You should set up class attribute for each text box
<input class="datepicker" type="text" />
and then
$( ".datepicker" ).datepicker({ dateFormat: 'yy-mm-dd' });
Or if you have only dates textboxes, you can attach datepicker for all textboxes by
$( 'input[type="text"]' ).datepicker({ dateFormat: 'yy-mm-dd' });
Another method, just for your reference, would be
$("#datepicker, #datepicker2").datepicker({ dateFormat: 'yy-mm-dd' });
For ANY textbox on the page:
$('input[type="text"]').datepicker({ dateFormat: 'yy-mm-dd' });
to get functional datepickers on different input boxes on the same page of a custom post with the least hassle and without messing with the jquery-ui.css and jquery-ui.theme.css or any other code of the jQuery ui library:
<input class="datepicker" name="first_intake" id="first_intake" value="" size="30" type="date">
<input class="datepicker" name="second_intake" id="first_intake" value="" size="30" type="date">
In the footer I initiate the datepicker in a function as I use wordpress and this page is in the admin backend, but you can just use the javascript snippet, if you do not use wordpress:
<?php
function my_admin_footer() { ?>
<script type="text/javascript">
jQuery(document).ready(function(){
jQuery('#first_intake.datepicker').datepicker({
dateFormat : 'd M, yy'
});
jQuery('#second_intake.datepicker').datepicker({
dateFormat : 'd M, yy'
});
});
</script>
<?php
}
add_action('admin_footer', 'my_admin_footer');
?>
Here's what worked for me...
PHP + jquery
$(function() {
for(let i = 1; i<= <?php echo count($rows) ?>; i++)
{
$( "#datepicker-popup"+i ).datepicker({ dateFormat: 'yy-mm-dd' });
}
});
Related
This question already has answers here:
Initialize JQuery Datepicker with a blank value
(2 answers)
Closed 7 years ago.
My code:
<html>
<head>
<script type="text/javascript">
$(function(){
$("#datepicker1").datepicker({
dateFormat: 'dd-mm-yy',
onSelect: function(selected){
$("#datepicker2").datepicker("option", "minDate", selected);
}
});
$("#datepicker2").datepicker({
dateFormat: 'dd-mm-yy'
});
});
$(document).ready(function(){
$('#reset').click(function(){
$('#datepicker2').datepicker("option", "minDate", null);
});
});
</script>
<body>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<table>
<tr>
<td>Start Date : </td>
<td><input type="text" name="startdate" id="datepicker1" /></td>
</tr>
<tr>
<td>End Date : </td>
<td><input type="text" name="enddate" id="datepicker2" /></td>
</tr>
<tr>
<td>
<input type="submit" id="submit" name="submit" value="Add" />
<input type="reset" id="reset" name="reset" value="Reset" />
</td>
</tr>
</table>
</form>
</body>
From above code, refer to below image:
First, I am going to choose start date is 7 Dec 2015 and end date is 8 Dec 2015, after that I choose the start date is 9 Dec 2015. I want the end date value is blank when I choose the start date is 9 Dec 2015. Is it possible to do it in Jquery? Can someone help me?
Change you script to this:
$(document).ready(function(){
$("#datepicker1").datepicker({
dateFormat: 'dd-mm-yy',
onSelect: function(selected){
$("#datepicker2").datepicker("option", "minDate", selected);
}
});
$("#datepicker2").datepicker({
dateFormat: 'dd-mm-yy'
});
$("#datepicker1").focusout(function(){
$('#datepicker2').val('');
});
$('#reset').click(function(){
$('#datepicker2').datepicker("option", "minDate", null);
});
});
jsfiddle
I am trying to develop a calendar input variable which also allows for a datepicker for the input boxes.
HTML
<select id="select">
<option value="Type">Type</option>
<option value="Range">Range</option>
<option value="Individual">Individual</option>
</select>
<div id="range" style="display:none;">
<input type="text" name="job_from" id="job_from" placeholder="From" class="datepicker" />
<input type="text" name="job_to" id="job_to" placeholder="To" class="datepicker" />
</div>
<div id="ind" style="display:none;">
<button type="button" id="add2" class="submit">Add Another Date</button>
<div id="item2">
<div><input type="text" class="datepicker" name="jobdates[]" /></div>
</div>
</div>
SCRIPT
$(document).ready(function(){
$("#add2").click(function (e) {
//Append a new row of code to the "#items" div
$("#item2").append('<div><input type="text" class="datepicker" name="jobdates[]" /><button class="delete submit" type="button">Delete</button></div>');
});
$("body").on("click", ".delete", function (e) {
$(this).parent("div").remove();
});
$('#select').change(function(){
if($('#select option:selected').text() == "Range"){
$('#range').show();
}
else{
$('#range').hide();
}
if($('#select option:selected').text() == "Individual"){
$('#ind').show();
}
else{
$('#ind').hide();
}
})
});
Here is the jsfiddle
when I take away the datepicker jquery function the hide/show functionality works, when i put the datepicker script in nothing works. Can someone explain why I cannot get both to work
Your code is fine, I believe you just weren't including the core jQueryUI files that the DatePicker needs in order to run.
Add this to the head of your page:
<link rel="stylesheet" href="//ajax.googleapis.com/ajax/libs/jqueryui/1.11.2/themes/smoothness/jquery-ui.css" />
<script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.11.2/jquery-ui.min.js"></script>
EXAMPLE 1
To address the issue where datepicker is not being initialized due to the new elements being written to the DOM after the jQuery has initialized the datepicker, you simply must call the datepicker when the user creates a new DOM element.
$("#add2").click(function (e) {
//Append a new row of code to the "#items" div
$("#item2").append('<div><input type="text" class="datepicker" name="jobdates[]" /><button class="delete submit" type="button">Delete</button></div>');
// This is where you need to add the datepicker jQuery again <<<<<<<<<
$(".datepicker").datepicker({ changeMonth: true, changeYear: true, numberOfMonths: 2, showWeek: true, dateFormat: 'dd-mm-yy'});
});
EXAMPLE 2
I think its not possible.
Make a second input (for datepicker) and show hide it if checked box...
The rest is js and css
EDIT:
$(document).ready(function(){
$("#add2").click(function (e) {
//Append a new row of code to the "#items" div
$("#item2").append('<div><input type="text" class="datepicker" name="jobdates[]"/><button class="delete submit" type="button">Delete</button></div>');
$(".datepicker").datepicker({ changeMonth: true, changeYear: true, numberOfMonths: 2, showWeek: true, dateFormat: 'dd-mm-yy'});
});
$("body").on("click", ".delete", function (e) {
$(this).parent("div").remove();
});
$('#select').change(function(){
if($('#select option:selected').text() == "Range"){
$('#range').show();
}
else{
$('#range').hide();
}
if($('#select option:selected').text() == "Individual"){
$('#ind').show();
}
else{
$('#ind').hide();
}
})
});
I like to insert a date of birth field in the php form? The range of values to be entered must be in between two years(ex:1988-12-20 to 1992-1-1). I am just a starter in php.<input type="date" name="dob">
You can do this between two years(ex:1988-12-20 to 1992-1-1)
<input type="date" name="bday" min="1988-12-20" max="1992-01-01" />
You can achieve this with html5 easily
<input type="date" name="bday" min="1990-01-02" max="2010-01-02" />
An alternative way is to use the Jquery Datepicker.
<html>
<head>
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script src="//code.jquery.com/ui/1.11.0/jquery-ui.js"></script>
<script>
$(function() {
$( "#datepicker" ).datepicker({
dateFormat: 'yy-mm-dd',
minDate: '1988-12-20',
maxDate: '1992-1-1'
});
});
</script>
</head>
<body>
<p>Date: <input type="text" id="datepicker"></p>
</body>
</html>
I am using JQuery autocomplete option to get values from database using PHP. I have 2 text boxes and I am trying to use same PHP file to get the required results.
My HTML code is
<tr>
<td>LOB</td>
<td><input autocomplete="off" type="text" name="ABC" class="autocomplete"/></td>
</tr>
<tr>
<td>Project Name</td>
<td><input autocomplete="off" type="text" name="PQR" class="autocomplete"/></td>
</tr>
and my JQuery is
$(document).ready(function() {
$( ".autocomplete" ).autocomplete({
source: 'LiveSearch.php?name='+ $(".autocomplete").attr('name') + '&',
minLength: 2
});
});
But as these there are 2 elements with same class. I am not getting correct arrr('name'). MySQL will differ based on this name. I tried (this) but still not getting the result.
If I am in the first text box then name should be of that text box etc..
Please let me know if I am missing anything.
You can target the textbox using name.
$('[name=ABC]').val()
Use id to differentiate
<tr>
<td>LOB</td>
<td><input autocomplete="off" type="text" name="ABC" id="id1" class="autocomplete"/></td>
</tr>
<tr>
<td>Project Name</td>
<td><input autocomplete="off" type="text" name="PQR" id="id2" class="autocomplete"/></td>
</tr>
//Script
$(document).ready(function() {
$( ".autocomplete" ).autocomplete({
source: 'LiveSearch.php?name='+ $("#id1").attr('name') + '&',
minLength: 2
});
});
You're mixing the scope of 'all autocompletes' and 'the current automplete'. One way of dealing with this is to use jQuery's each() method:
$('.various-things').each(function () {
console.log($(this).attr('something');
});
This would log the 'something' attribute of each .various-things on the page. It looks like what you'd need is:
$(document).ready(function() {
$( ".autocomplete" ).each(function () {
$(this).autocomplete({
source: 'LiveSearch.php?name='+ $(this).attr('name') + '&',
minLength: 2
});
});
});
Now it'll find all autocompletes and set them up using their own name.
before ajax call I write the code for date picker is:
<script type="text/javascript">
$(function() {
$("#birthdate").focus(function() {
$(this).datepicker().datepicker( "show" )
});
});
$(function() {
$("#joindate").focus(function() {
$(this).datepicker().datepicker( "show" )
});
});
</script>
this function called after onfocus event:
<tr>
<td align="left">
<input type="text" id="birthdate" name="birthdate" onfocus="$('#birthdate').datepicker({changeMonth: true,changeYear: true,dateFormat: 'dd/mm/yy',defaultDate: '-20y -1m -247d',yearRange: '-50:+0'});" tabindex="14" style="width:300px; " value="<? if($get_emp!='0')echo $employee_birth; ?>" /><span style="color:#F00;">*</span></td>
<td>
<input type="text" name="joindate" id="joindate" onfocus="$('#joindate').datepicker({changeMonth: true,changeYear: true,dateFormat: 'dd/mm/yy',defaultDate: '-3y -1m -247d',yearRange: '-50:+0'});" tabindex="15" style="width:300px; " value="<? if($get_emp!='0')echo $employee_join; ?>"/><span style="color:#F00;">*</span>
</td>
</tr>
after ajax call I call the same code but it not opens on first click , however it opens on second click?
please help me to sort out this problem..........................
Use this javascript. You only need to initialize the datepicker() once
$(function() {
$('#birthdate').datepicker({changeMonth: true,changeYear: true,dateFormat: 'dd/mm/yy',defaultDate: '-20y -1m -247d',yearRange: '-50:+0'});
$("#joindate").datepicker({changeMonth: true,changeYear: true,dateFormat: 'dd/mm/yy',defaultDate: '-3y -1m -247d',yearRange: '-50:+0'});
$("#birthdate, #joindate").focus(function() {
$(this).datepicker( "show" )
});
});
I removed the focus attribute from the input tags:
<input type="text" id="birthdate" name="birthdate" tabindex="14" style="width:300px; " value="<? if($get_emp!='0')echo $employee_birth; ?>" /><span style="color:#F00;">*</span></td>
<input type="text" name="joindate" id="joindate" " tabindex="15" style="width:300px; " value="<? if($get_emp!='0')echo $employee_join; ?>"/><span style="color:#F00;">*</span>
Try something like this instead:
$(document).ready(function(){
$("#joindate").datepicker({
// Options and methods here
});
$("#joindate").focus(function(){
$("#joindate").datepicker("show");
});
$("#joindate").focus();
});
You can also do a $.post() on the "onSelect" event, should you want to post the date on the actual click event of the date, instead of having to tie it to a form submit.