How to position a JQueryUI datepicker dialog via a MouseEvent - jquery-ui

I'm trying launch a datepicker in dialog mode via clicking on some text.
<input type='hidden' id='datepicker'>
<div onclick="getdate()">
Click on me to launch DatePicker dialog
</div>
where
function getdate() {
$( "#datepicker" ).datepicker("dialog", "10/12/2013", function(){alert("wibble");},{dateFormat: "dd/mm/yy"},[20,20]);
}
Default dialog behaviour (no position) is fine (pops up centre screen), apart from Firefox which goes for top centre. Specifying position as Number[2] works fine as well.
The JQueryUI api documentation says:
pos
Type: Number[2] or MouseEvent
The position of the top/left of the dialog as [x, y] or a MouseEvent that contains
the coordinates. If not specified the dialog is centered on the screen.
Unfortunately, they have no example of using a MouseEvent and I fear that I am reaching the limits of my ability in this area. I tried various formats such as:
$( "#datepicker" ).datepicker("dialog", "10/12/2013", function(){alert("wibble");},{dateFormat: "dd/mm/yy"},$.Event("mouseclick"));
to no avail. Googling for datapicker dialog method seems to bring up a plethora of websites happy to parrot the api documentation without expanding on it.
How does one pass in a MouseEvent to this method?
Update
Based on j08691's answer, this is the syntax that I was looking for:
$( "#datepicker" ).datepicker(
"dialog",
"10/12/2013",
function(){alert("wibble");},
{dateFormat: "dd/mm/yy"},
event
);
Update 2
A bit more diddling on another computer with different versions of some browsers and a bit more research has led me to the following as my final answer:
<input type='hidden' id='datepicker'>
<div onclick="getdate(event)">
Click on me to launch DatePicker dialog
</div>
function getdate(e) {
e = e || window.event;
$( "#datepicker" ).datepicker(
"dialog",
"10/12/2013",
function(date){changeDate(date);},
{dateFormat: "dd/mm/yy"},
e
);
}
(where changeDate is another function)

It looks like the reason that there's not much on the web about this is that the only semi-useful, cross-browser event object mouse properties that you would use are event.pageX and event.pageY.
For example:
function getdate() {
$("#datepicker").datepicker("dialog", "10/12/2013", function () {
alert("wibble");
}, {
dateFormat: "dd/mm/yy"
}, [event.pageX, event.pageY]);
}
See this jsFiddle example and click on the grey div. The datepicker will be placed at the coordinates of where the mouse clicks on the div.

Try this :
<div onclick="getdate()">
<input type='hidden' id='datepicker'>
Click on me to launch DatePicker dialog
</div>

Related

Jquery mobile 1.4 on iOS the fixed elements on bottom of page hide input text onfocus

Hi there I am building cordova 3 app with Jquery mobile 1.4
Safari, and Chrome on my mac have no issues with the following, but when I deploy on the real device, or iOS simulator and want to type in the fields I get the bottom fixed elements of the page moving up and overlapping the input elements. The cursor is blinking in the right position, where the input field is, but the slider of the footer is hiding it.
To make it simple my page looks like that:
[header fixed]
div with input text
div with input text
div with input text
[footer fixed]
one of the input text instances
<input data-role="none" class="inputCalc gray_br" type="text" id="grams4" value="37"><div class="macro_g">grams</div>
the slider on the bottom
<div id="cal_slider" style="position:fixed; bottom:0px; left:0px; height:57px; width:100%; background-color:#E2E2E2;">
<form style="padding-top:6px" class="full-width-slider"><label for="slider-12" class="ui-hidden-accessible">Slider:</label> <input type="range" data-highlight="true" name="slider-12" id="slider-12" min="0" max="100" value="50"></form>
</div>
Jquery to show hide on focus and blur events
$(document).on('focus', 'input , text', function(e){
// I have try with --> $("#grams4").focus( function () {... // but it's the same
console.log("on focus fired");
$("#cal_slider").hide();
});
$(document).on('blur', 'input, text', function(e){
console.log("on blur fired");
$("#cal_slider").show();
});
So I have been trying with focus() and blur() events to toggle (show and hide) the slider. It works fine on Chrome and Safari.
But on real device (iPhone 5 iOS7) when I tap the input textfield, the cycle works like expected the first time only:
(1) I get a focus log, the footer is hidden, (2) the keyboard comes up and (3) I can type in the input text.
Then (4) when I tap DONE the keybord hides and it's OK. (5) I get a blur event in the console, and the footer with the slider shows again. Perfect.
However, if I tap again on any of the input text fields, I get a blur log in the console and not a focus as expected, and the slider goes back again in front of the input field.
As I told above in Chrome, Safary works perfectly.
Any other ideas how to detect keybord on / off, maybe without event handlers ?
This works:
put the following code after the closing tag of the multipage.
</body>
<script type="text/javascript">
document.write( '<style>#footer{visibility:hidden}#media(min-height:' + ($( window ).height() - 10) + 'px){#footer{visibility:visible}}</style>' );
</script>
</html>
Thanks to Richard Kennard stackoverflow.com/a/20092755/3166158

Using jquery ui dialog with tooltip causes tooltip display on opening, not hover

I set up the tooltip and dialog like so:
$(document).ready(function() {
$( "#dialog" ).dialog({ autoOpen: false });
$( document ).tooltip();
but when i open the dialog later its close tooltip always appears on opening, NOT just on hovering over close as expected. Has anyone else seen this behaviour/knows why it occurs?
Setting the items option to exclude the dialog's titlebar close widget seems to work well for me in jQueryUI 1.9+
$( document ).tooltip({
items: '*:not(.ui-dialog-titlebar-close)'
});
Found a solution:
$( "*" ).tooltip();
$('.ui-dialog-titlebar-close').tooltip('disable')
works in place of the above
Tooltip appears because a button automatically gets focus when a dialog opens (this is a strange behavior). You need to add an attribute "tabindex" to any element in the dialog to avoid this.
For example:
<table tabindex="1">
According to dialog's documentation:
Upon opening a dialog, focus is automatically moved to the first item
that matches the following:
The first element within the dialog with the autofocus attribute
The first :tabbable element within the dialog's content
The first :tabbable element within the dialog's buttonpane
The dialog's close button
The dialog itself
So my solution was to add autofocus to an empty div at the top of the form I was using in my dialog:
<form action="" method="post" accept-charset="utf-8">
<div class="stealFocus" autofocus></div>
Good solution for me with 1.11.3 jQuery and 1.10.4 jUI
$.ui.dialog.prototype._focusTabbable = function(){};
It will desactivate autofocus and dont see auto popup anymore
I use :
$( ".selector" ).dialog({ closeText: "" });

Jquery UI Tool Tip Trigger with Delayed Release

I am trying to use the jQuery Tool tip function and from the examples I am having issues altering the code for the function I am after.
http://jqueryui.com/tooltip/#forms
I have several coulored Circle area maps that I want a tool tip to pop up from when one is clicked on.
The following function works but it also pops up a tool tip when mousing over the element. Then when you click on it another tool tip pops up.
So What I need, is to change the function to only pop up "on click" and then to fade away after a few seconds.
jQuery:
$(function() {
var tooltips = $( "[title]" ).tooltip();
$( "#01-001" ).click(function() {
tooltips.tooltip( "open" );
})
});
HTML:
<area id="01-001" shape="circle" coords="99,71,10" href="#" alt="01-001" data-maphilight='{"stroke":false,"fillColor":"FF0000","fillOpacity":1,"alwaysOn":true}' title="Tool Tip Messgae." />
Thanks Guys.
Samuel
The simplest solution would be to wait until the click event to instantiate the tooltip. You're getting them on hover because you instantiated them all at once.
There's a detailed answer by here.

jQuery UI autocomplete: dropdown disappears when Chrome loses focus

(Note I have not yet tested this in IE FF or Safari, I'm hoping there is some setting on the autocomplete to make this work...)
In Chrome, when I type some text into a box that triggers a jQuery UI autocomplete dropdown to appear, the dropdown disappears when I switch focus to another application (my programming app Visual Studio).
Is there a setting to make the autocomplete drop down menu "stick" and remain visible when the browser loses focus in the operating system?
When you switch to another application an onblur event will be fired by the browser which is what hides the autocomplete menu. Your best bet to accomplish this "sticky" behavior is to attach an onfocus event handler to the window and open the autocomplete menu then.
$(function() {
var autocomplete = $( 'whatever' ).autocomplete();
$( window ).on( 'focus', function() {
autocomplete.autocomplete( 'search' );
});
});​
I have a live example of this here - http://jsfiddle.net/RmALY/1/show/.

jQuery UI Datepicker Today Link

I'm using the jQuery UI Datepicker for a project, but need the today button to enter the date into the textfield when a user clicks on it. By default it just selects the date but doesn't enter it into the field. I'm guessing this will just require a quick mod of my jQuery UI file, but what do I change? Thanks.
Edit: I tried this: http://dev.jqueryui.com/ticket/4045 but it doesn't work!
I found a solution that works here: http://forum.jquery.com/topic/jquery-ui-datepicker-today-button. Just use this snippet of jQuery (I just put it in with my datepicker init code):
$('button.ui-datepicker-current').live('click', function() {
$.datepicker._curInst.input.datepicker('setDate', new Date()).datepicker('hide');
});
The only problem I had was that focus would remain in the input box after clicking the "today" button, which means you cant click it again to select a different date. This can be fixed by suffixing blur():
$('button.ui-datepicker-current').live('click', function() {
$.datepicker._curInst.input.datepicker('setDate', new Date()).datepicker('hide').blur();
});
I also didn't like the styling on the "today" button - maybe it was just my theme or something, but the today button was kind of greyed out compared to the "done" button. This can be fixed with the following CSS (may vary for different themes, i'm not sure):
/* This edit gives five levels of specificity, thus making it override other styles */
.ui-datepicker div.ui-datepicker-buttonpane button.ui-datepicker-current {
font-weight: bold;
opacity: 1;
filter:Alpha(Opacity=100);
}
I realize this is somewhat of a late reply, but I just ran in to this issue and the solution proved works like a charm.
However, the button-styling issue is caused by jQuery ui classes.
I added the following code to the click action of the date text-input:
$('.date').click(function() {
$('button.ui-datepicker-current').removeClass('ui-priority-secondary').addClass('ui-priority-primary');
});
This removes the wrong class, and adds the correct class to the button, making it the same as the "Done" button.
It should not matter what theme you are using.
For completeness, here's my entire code:
$('.date').datepicker({
dateFormat: 'yy-mm-dd',
showButtonPanel: true
}).click(function() {
$('button.ui-datepicker-current').removeClass('ui-priority-secondary').addClass('ui-priority-primary');
});
$('button.ui-datepicker-current').live('click', function() {
$.datepicker._curInst.input.datepicker('setDate', new Date()).datepicker('hide').blur();
});
Just add a <input type="text" class="date" value="" />, and you're good to go.

Resources