Click here to Skip to main content
15,888,351 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
is it possible to call a javascript keydown function for a textbox when a button is clicked?
Posted
Comments
jaket-cp 22-Oct-15 6:12am    
simulate keydown event javascript
google is your friend:
http://lmgtfy.com/?q=simulate+keydown+event+javascript
http://stackoverflow.com/questions/596481/simulate-javascript-key-events
Kats2512 22-Oct-15 6:22am    
sorry I forgot to mention that I did a lot of googling but did not find what I desire.
Krunal Rohit 22-Oct-15 8:50am    
$(function(){
$("button").click(function() {
$("input").trigger("keydown").promise().done(function(){alert('asd')});
});
});

Is it what you're looking for ?

-KR
Sergey Alexandrovich Kryukov 22-Oct-15 14:39pm    
This is not the best approach; and the inquirer does not want to use jQuery. See the comment below and also Solution 1.
—SA
Kats2512 22-Oct-15 10:43am    
this is jquery, i need javascript...

1 solution

Please see my comment to the question.

So, let me continue from this point. Instead of "calling a handler", which would be who knows what, you write some function. Call this function from two event handlers, or more, say, one would be a keydown event handler of one element, and another would be, say, click event handler of your button. This way, you will include identical functionality in two different event handlers, of the same element or different elements.

By the way, if you use "this" in your handlers, don't forget that they are different "this", different references, so be sure to pass appropriate reference to your function. If in your function you also want to use "this", you can use yourFunction.call or yourFunction.apply to pass it explicitly:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/call[^],
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/apply[^].

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900