0

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.
}
2
  • 1
    $('#itemselected').click(function() { Commented Apr 19, 2016 at 4:34
  • Do you expect something like this? Commented Apr 19, 2016 at 5:52

3 Answers 3

1
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);
}
Sign up to request clarification or add additional context in comments.

Comments

0

Try to trigger the keypress on click

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

3 Comments

what exacly is not working, do you have errors in your console?
I don't get any error while running above code. Can see input as object in console.
I can give you my skype if we can talk ?
0

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.
});

Comments

Your Answer

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.