code_junkie

Trigger a hyperlink with keystroke

Nov 14th, 2011
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.68 KB | None | 0 0
  1. <html>
  2. <head>
  3. <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.1/jquery.min.js"></script>
  4. <script type="text/javascript">
  5. $(document).ready(function(){
  6. $("a").click(function(event){
  7. alert(this.href);
  8. });
  9. });
  10. function test(event){
  11. if (event.keyCode == 13) {
  12. $("a").addClass("test");
  13. $("a.one").trigger('click');
  14. }
  15. else {
  16. $("a").removeClass("test");}
  17. }
  18. </script>
  19. <style type="text/css">
  20. a.test { font-weight: bold; }
  21. </style>
  22. </head>
  23. <body onkeypress='test(event)'>
  24. <a class="one" href="http://jquery.com/">jQuery</a>
  25. <br/>
  26. <a class="two" href="http://stackoverflow.com/">stack overflow</a>
  27. </body>
  28. </html>
Add Comment
Please, Sign In to add comment