Showing posts with label javascript. Show all posts
Showing posts with label javascript. Show all posts

Monday, November 16, 2009

jQuery Dialog Input Focus

I'm a big believer in setting keyboard focus to the proper input. As a long-time laptop user, I avoid using the mouse wherever possible. It simply slows me down. Save the mice!

Using jQuery UI's dialog to pop up a form, I could not do this for some reason. Here is my code to set input focus in a dialog.


// In event handler
$('#mydialog').open();
$('#myinput').get(0).focus();


And nothing! I tried with many dialog options, and the only thing I could do to make it work, was to eliminate the dialog.

The only thing I can figure is that the dialog itself is setting focus, or blurring my focus. So I fixed this by delaying the focus call using a timer.


// In event handler
$('#mydialog').open();
setTimeout("$('#myinput').get(0).focus();", 500);


Note, times less than 200 ms were not reliable.

Thursday, October 29, 2009

jQuery workaround for click exclusion

I've been working with jQuery for a short time now. I would think there is a better way to do this, so I'm looking for help.

I have a <div> which contains an <a href="...">

In my javascript, I handle click events on the div, but don't want to handle the a clicks (want the link to be followed).

I would like to express

$('div-expression-excluding-the-hyperlink').click( function {
// do something
});



but I don't know how to do that. Instead I have


link_clicked=false;
$('#a-id').click( function {
link_clicked = true;
});
$('#div-id).click( function {
if (!link_clicked)
// do things
}
link_clicked = false;
});



This works, but I'm not sure its reliable/portable,and I think there was a much better way to either filter out the a or determine the type of what was actually clicked in the callback. I think this references a <div> though.