Reputation: 6756
This is action performed when I click on button:
$('div.my-button').on('click', 'button', function(e) {
// some code
});
How can I call this event when performing another action, e.g. on keypress? Should I move code of this event to separate function or I can call a click or something?
I tried to call click:
$('div.my-button').click()
but the action was not performed.
I use jquery 1.9+
EDIT 1:
$('div.my-button').on('click', 'button', function(e) {
var $this = $(this);
var request = $.ajax({
url: "/my_url/",
type: "POST",
data: {
"value": $this.val(),
},
success: function(result){
//some actions
},
error: function(result){
//some actions
}
});
});
I try to call this using:
jQuery(document).bind('keydown', 's',function (e){
$('div.my-button').trigger('click');
});
For binding keys i use jquery.hotkey
.trigger does not work for me.
Upvotes: 0
Views: 57
Reputation: 36531
use trigger()...
Any event handlers attached with .on() or one of its shortcut methods are triggered when the corresponding event occurs. They can be fired manually, however, with the .trigger() method. A call to .trigger() executes the handlers in the same order they would be if the event were triggered naturally by the user:
$('div.my-button button').trigger('click') ;
Upvotes: 2
Reputation: 388336
Use .triggerHandler as you may not want the default click behaviour or event propagation to happen
$('div.my-button button').triggerHandler('click') ;
Upvotes: 2