Lalit Sharma
Lalit Sharma

Reputation: 565

Call keypress event on button click?

I have below jquery code which is execute on keypress but I would like to execute same on button click. Please help me.

$('#itemselected').live('keypress', function() { 
      //some code which using $(this) also.
}

Upvotes: 0

Views: 167

Answers (3)

or hor
or hor

Reputation: 723

var myFunction = function(event){
   console.debug(event);
   //do your stuff here
};
    $('#itemselected').on('keypress', function(event) { 
      myFunction(event);
}
$('#itemselected').on('click', function(event) { 
      myFunction(event);
}

Upvotes: 1

damo-s
damo-s

Reputation: 1008

I think you can just add 'click' to the list of event types like so:

$('#itemselected').on('keypress click', function() { 
      //some code which using $(this) also.
});

Upvotes: 0

madalinivascu
madalinivascu

Reputation: 32354

Try to trigger the keypress on click

$('button').click(function() {
$('#itemselected').trigger('keypress');
});

Upvotes: 0

Related Questions