jQuery UI - Button inside dialog keeps getting the focus - jquery-ui

So I have a jQuery UI dialog with some buttons inside (full example here => http://jsfiddle.net/VLr5G/3/):
<div id="test">
<button>Btn 1</button>
<button>Btn 2</button>
<button>Btn 3</button>
</div>
What I want to do is force the focus on the "Close" button - I have tried applying the following code when the dialog opens:
open: function() {
$(this).parents('.ui-dialog-buttonpane button:eq(0)').focus();
}
Unfortunately the focus always keeps getting on the first button inside the dialog. Is this a bug, or am I missing something ?
Thanks a lot in advance for your help !
UPDATE
Okay so the answer from Stanley works fine with my first example... But try to change the version of jQuery UI => http://jsfiddle.net/VLr5G/10/
From what I could find so far, it worked until jQuery UI 1.10.0.

You are not getting the close button correctly.
You should do this instead:
$(document).ready(function() {
$('#test').dialog({
buttons: {
'Close': function() {$(this).dialog('close');}
},
open: function() {
$(this).parent().find('.ui-dialog-buttonpane button:eq(0)').focus();
}
});
});
Working jsfiddle: http://jsfiddle.net/GG7EP/2/
UPDATE
To make it work with jQuery 1.10.0 or above, call the button's focus function in focus event
$(document).ready(function() {
$('#test').dialog({
buttons: {
'Close': function() {$(this).dialog('close');}
},
focus: function() {
$(this).parent().find('.ui-dialog-buttonpane button:eq(0)').focus();
}
});
});
JsFiddle: http://jsfiddle.net/V3P4t/

Related

jQuery Mobile Slider create event not firing

First time I am using jquery mobile the slider create event seems to not want to fire.
According to jQuery Slider page the following events should work (create/start/stop), I did get start/stop to work but not the create, code below:
<script>
$(function () {
$("#slider-s").slider({
create: function (event, ui) {
console.log("creating");
},
start: function (event, ui) {
console.log("start moving");
},
stop: function (event, ui) {
console.log("stop moving");
}
});
});
</script>
<input type="range" name="slider-s" id="slider-s" value="25" min="0" max="100" data-highlight="true">
So every time I move up the slider I get start moving and when I stop it I get stop moving but I thought I would get creating once the slider has been loaded/created.
Working example: http://jsfiddle.net/Gajotres/6dQ9E/
The point is to use proper page event to detect right widget event. Also you should never used classic document ready with jQuery Mobile, sometimes it works just fine and sometimes it behaves extremely weird. That's why jQuery Mobile page events exist. Read more abut it here.
Javascript used:
$(document).on('pagecreate', '#index', function(){
$(document).on('slidecreate', '#slider-s', function(){
console.log("create");
});
$(document).on('slidestart', '#slider-s', function(){
console.log("start");
});
$(document).on('slidestop', '#slider-s', function(){
console.log("stop");
});
});

JQuery UI Sortable: how to make items have droppable behavior (ex. hoverClass)?

I am using JQuery UI's drag and drop for a web application.
I have the following HTML structure:
<div id="list">
<div class="item">1</div>
<div class="item">2</div>
<div class="item">3</div>
</div>
and Javscript:
$('#list').sortable({containment: 'parent'});
When I drag, move, and drop an item, all items are aligned nicely verticially, which I like it.
Now I hope that when I drag and hover an item over another item, the item below can show some visual effects similar to the hoverClass available in JQuery UI's Droppable.
How can I do it?
Thanks and regards!
For the hoverClass you can do something simiar with the over and out methods. The start and stop could also help you.
$('.sortable').sortable({
over : function(){
$(this).addClass('valid');
},
out : function(){
$(this).removeClass('valid');
}
});
As stated before, $(this) points to the current sortable you are hovering on a over typed event.
The most reliable way of implementing sortable hovering with jQuery is as follow:
$('#some-element').sortable({
start: {
ui.item.parent().addClass('sortable-hover');
},
over: {
$('.sortable-hover').removeClass('sortable-hover');
$(this).addClass('sortable-hover');
},
stop: {
ui.item.parent().removeClass('sortable-hover');
}
});
You can add more logic as required.
Hope it helps,
-- José
I hope that's what you are looking for
$('#list').sortable({
containment: 'parent',
start: function() {
$(this).addClass('sortable-hover');
},
over: function() {
$('#list').removeClass('sortable-hover');
$(this).addClass('sortable-hover');
},
stop: function) {
$('#list').removeClass('sortable-hover');
},
});

pass variable to jquery dialog

I am wanting to pass a variable to a jquery dialog window. I am using classic ASP and have onyl really touched on the world of jquery. I have spent a few hours researching and tring to use the .data method but are having trouble. Your time and assitance would be very much apriciated!
Here is my stripped down page:
$(function(){
// Dialog
$('#dialog').dialog({
autoOpen: false,
show: "slide",
hide: "fade",
width: 452,
modal: true,
buttons: {
"Ok": function() {
PostItNote.submit();
$(this).dialog("close");
},
"Cancel": function() {
$(this).dialog("close");
}
}
});
// Dialog Link
$('#dialog_link').click(function(){
$('#dialog').dialog('open');
return false;
});
});
and this is how I call the window:
<a href='#' id='dialog_link' CLASS='OrangeButton'> Show Window </a>
and the contents of my window:
<div ID="dialog" TITLE="Periodic Report">
stuff here
</div>
Why can I not do this:
<a href='#' id='dialog_link(someValue)' CLASS='OrangeButton'> Show Window </a>
Thank you in advance
How it is used in the ASP loop is:
do until Products.EOF
--other code here---
<a href='#' id='dialog_link(Products(0))' CLASS='OrangeButton'>
--other code here---
products.moveNext
loop
The dialog is just a div on your page, so it can't really be "passed" a value. It can be manipulated by any JavaScript variables in scope though. You could change your click handler to use a variable to manipulate the dialog:
var myVariable = "Some new text for the dialog";
$('#dialog_link').click(function(){
//New code to "pass" a value to the dialog
$('#dialog').text(myVariable);
$('#dialog').dialog('open');
return false;
});
Or you could use the open member of the dialog:
...
width: 452,
open: function() { $('#dialog').text(myVariable); },
modal: true,
...
To make changes to the dialog div whenever it is opened.
The code id='dialog_link(someValue)' will not do anything, as the id attribute cannot make function calls, only event handlers like onchange can do that. Even if it could, dialog_link is not a function that can be passed a value. It is just the id of another element.
I'm sure you're probably already aware, but the jQuery documentation is very useful- here are the docs for dialog.
Edit
In response to your comment: I would drop the $('#dialog_link').click(); statement and change the link code to the following:
<a href='#' class='OrangeButton' onclick='openDialog(someValue);'>Show Window</a>
And then add the JavaScript function to be called on the click event:
function openDialog(value) {
$('#dialog').text(value);
$('#dialog').dialog('open');
}
Edit 2
Inside of the ASP loop something like this should do the trick:
Response.Write("<a href='#' onclick='openDialog(" & Products(0) & ");'>Show</a>")
This will create an <a></a> element on the page with an onclick handler that passes the desired value to openDialog, making it accessible by the dialog.
I changed the code for the link slightly so that it all fit on one line, but you could always add back the class='OrangeButton' and all that if you'd like.

showing jquery UI Dialog on focus won't focus the element

I want to display an jquery UI dialog when a user focues a certain input box. It works the problem is, that the input box won't get the focus after closing the dialog, meaning user clicks into input box, dialog is opened. User closes dialog -> input box does not have focus.
The only way to actually focus the input box is ti click into it a second time while the dialog is already displayed. This is pretty annoying. i would like the inputbox to have focus after closing the dialog.
$(function() {
$( "#identifiersDialog" ).dialog({autoOpen: false});
});
$('#identifiers').focus(function(event) {
$( "#identifiersDialog" ).dialog('open');
});
You could use
$(function(){
InitiliseInput();
});
function InitiliseInput(){
$('#identifiers').bind('focus', function() {
$('#identifiersDialog').dialog({ close: function() {
$('#identifiers').unbind('focus').focus().blur(function(){ InitiliseInput()});
}});
});
}
Working example here http://jsfiddle.net/RCBQs/1/
The blur function then resets the dialog once the focus has moved away from the input, so that refocusing opens the dialog again.
This should help.
$(function() {
$( "#identifiersDialog" ).dialog({autoOpen: false, close: function() { $('#identifiers').focus() }});
});
$('#identifiers').focus(function(event) {
$( "#identifiersDialog" ).dialog('open');
});

Overriding jQuery Dialog method

I am trying to override close method of Jquery Dialog method.
Code :
jQuery.Dialog.close = function() {
alert('my close');
}
But its not working. Please help.
There is an event called beforeClose which would allow you to do what you want, I think. When it fires, you can hide the dialog, then return false, which would prevent the dialog from actually closing.
$( ".selector" ).dialog({
beforeClose: function(event, ui) {
$(this).hide();
return false;
}
});
Reference: http://jqueryui.com/demos/dialog/ under the Events tab below the example
You're setting it up wrong. Check this out to see how to do it correctly.
Ok, so that link doesn't take you where I thought it would. Here's the relevant bit from jqueryui.com.
closeType:dialogclose
This event is triggered when the dialog is closed.
Code examples
Supply a callback function to handle the close event as an init option.
$('.selector').dialog({
close: function(event, ui) { ... }
});
Bind to the close event by type: dialogclose.
$('.selector').bind('dialogclose', function(event, ui) {
...
});

Resources