1

The alert is working, but the button just won't click...

$('#loginDialog .field input').keyup(function (e) {
     if (e.keyCode == 13) {
         alert('it is working!');
         $('.ui-button').click();
         return false;
     }
});

I have tried many different things, including reinitializing the method when the dialog gets opened, but nothing seems to work...

Html:

<div id="loginDialog" title="Please Login">
     <div class="label">Password:</div>
     <div class="field"><input type="password" /></div>
</div>
user1477388
  • 20,790
  • 32
  • 144
  • 264
  • 1
    Where are the elements with class of `ui-button`? Can you post that HTML too please? Are they dynamically added to the DOM? If so, are you using delegate event bindings? Are you running the code inside a `document.ready()`? – Nope Mar 26 '13 at 15:28
  • 1
    is your javascript running after $(document).ready()? – Adrian Mar 26 '13 at 15:29
  • Good questions. It is inside dom ready and the ui-button is generated by jquery ui – user1477388 Mar 26 '13 at 15:31
  • @user1477388: If the button is generated what ever click event you have would propably need to be bound using event delegation, ie: `$(closestStaticElement).on('click', '.ui-button', function(){...)` – Nope Mar 26 '13 at 15:32
  • How many elements have the ui-button class? I've usually had to do a "button:contains(...)" kind of thing to narrow down the actual button. – dev4life Mar 26 '13 at 15:35

3 Answers3

3

the ui-button is generated by jquery ui

I'm assuming from your comment that the button is generated dynamically and that any click event you have bound to is will have to be bound using event delegation, similar to:

$('body').on('click', '.ui-button', function(){...)

Instead of body, using the closest static element will work as well and would be preferred.

Nope
  • 22,147
  • 7
  • 47
  • 72
  • Thanks Francois. In jquery there are many ways to solve things, so this probably works, too. Netme's solution is the one that worked, first, however. – user1477388 Mar 26 '13 at 15:35
2

Please, try this:

$(function() {
$('#loginDialog .field input').keyup(function (e) {
     if (e.keyCode == 13) {
         alert('it is working!');
         $('.ui-button').trigger('click');
         return false;
     }
});

$('.ui-button').click(function() {
   alert('hello world'); 
});
};

Here there is an example: http://jsfiddle.net/netme/YZH3B/

Mikhail Chernykh
  • 2,659
  • 22
  • 15
2

This should trigger the event ...

 $('.ui-button').trigger('click');
Lemonade
  • 503
  • 2
  • 8