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>
asked Mar 26, 2013 at 15:26
5
  • 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()? Commented Mar 26, 2013 at 15:28
  • 1
    is your javascript running after $(document).ready()? Commented Mar 26, 2013 at 15:29
  • Good questions. It is inside dom ready and the ui-button is generated by jquery ui Commented Mar 26, 2013 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(){...) Commented Mar 26, 2013 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. Commented Mar 26, 2013 at 15:35

3 Answers 3

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.

answered Mar 26, 2013 at 15:34

1 Comment

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.
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/

answered Mar 26, 2013 at 15:33

5 Comments

This works (Will accept soon). So, trigger huh? Why not click?
click() works also. I think, the real problem is with documentready.
No, I was in doc ready before. The solution was simply changing click to trigger and I am interested to know why.
There are some explanations here: stackoverflow.com/questions/2954932/…
But anyway, according to manual click() is just a shortcut to trigger('click')
2

This should trigger the event ...

 $('.ui-button').trigger('click');
answered Mar 26, 2013 at 15:38

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.