0

How can we call the trigger click event on a live object.

$("#continue").live("keypress",function(){
 if (e.which == 32 || e.which == 13) {
    $(this).trigger("click");
  }
});

when i press enter on the button it is coming into if block but it is not triggering the button. can u please help me in this. Thanks in advance.

ThiefMaster
  • 310,957
  • 84
  • 592
  • 636
shaik
  • 11
  • 1
  • 3

3 Answers3

2

You use the argument "e", but never actually pass it in as an argument.

$("#continue").live("keypress",function(){

needs to be:

$("#continue").live("keypress",function(e){
Athena
  • 3,130
  • 1
  • 19
  • 17
  • thanks . i am doing the same as you explained. but it is not working – shaik Mar 10 '11 at 11:21
  • Not passing the event is an issue with the code, but that alone will not fix the problem. Not all versions of IE support e.which and also doesn't (correctly mind you) fire onpress events for "Special Keys" as I mention below. – Jeremy Battle Mar 10 '11 at 11:42
  • It never even occurred to me to think of IE since it wasn't mentioned in the question (*grin*) have upvoted yours; that's great deductive work. – Athena Mar 10 '11 at 12:19
2

Try this:

$("#continue").live("keyup",function(e){
 if (e.keyCode == 32 || e.keyCode == 13) {
    $(this).trigger("click");
  }
});

I covered this in this post a bit: EnterKey is not working sometimes in IE8, using jQuery's keyPressed

But basically there are "Special Keys" that won't fire on keypress and you have to use keydown or keyup.

Example here: http://jsfiddle.net/uS4XK/1/

Though I have to be honest this is a strange behavior to want. You want a click event on #continue when they press enter in it?

Community
  • 1
  • 1
Jeremy Battle
  • 1,638
  • 13
  • 18
0

You need to check if the $(this) really matches your button.

Try replacing :

$(this).trigger('click')

With

$('#continue').trigger('click')
Jerome WAGNER
  • 21,986
  • 8
  • 62
  • 77
  • if you give like this $('#continue').trigger('click') its working . but if we give $(this) is not working. I am trying to write more than four id's in a single click event. – shaik Mar 10 '11 at 11:19