Click here to Skip to main content
15,916,188 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
I am new to complex jquery and javascript. I am doing maintenance on aspx pages with multiple panels and user controls. Each panel has one or more buttons with the specification that the Enter key will be the default key no matter which button has focus to meet 508c Compliance. I have tried: setting the default key for each panel, and using keydown with a div. The research so far sends a single button id with the event to a jquery or javascript. The problem with these is each fires a already known button id. OR performs the default key event no matter which button has focus. What the script needs to do is: When the Enter Key is hit, the button event for the button that has focus will fire without using the mouse to perform the "click" event, no matter which button it is. I realize that parsing the document.elements is an option, however very response time consuming. I see document.ActiveElement is being suggested, however I do not have access to this property at the coding level that I am maintenancing. Thank you for any help I can get.
Posted

1 solution

It's a bad idea to simulate a button click, and a known anti-patten in UI development.

You will see the right approach if you consider the purpose of it. You don't really need "to fire a specific button". What you really need to do, it to call same function as the one called when the user clicks a button.

Just take it and make it — exactly in this way. Define some function and call it from two (or more) different places of the code. One would be from the event handler of some button click, another one — elsewhere. Only in this way you will achieve reasonable UI consistency in invocation of some actions.

[EDIT]

One of the ways to get a focused element is described here:
http://stackoverflow.com/questions/7329141/how-to-get-current-focused-element-in-javascript[^].

Also, pay attention for jQuery: it can help you a lot:
http://en.wikipedia.org/wiki/Jquery[^],
http://jquery.com/[^].

—SA
 
Share this answer
 
v4
Comments
Member 9516045 15-Oct-12 17:57pm    
WHEN the Enter key is hit, IF a button has focus, its click event must fire. The case is that user will NEVER "click". The user will use a keyboard ONLY. Tab keys will be used to move from element to element; WHEN the user hits either Enter key, the page must determine which if any button has/had focus and execute its event.

Yes, I am beating my head against a wall. The biggest problem is determining which button has focus WHEN the Enter key is hit. There will always be more than one button, up to n-buttons on a page. I know how to capture WHEN the Enter key is hit. I know how to call the button's click event. I just do not know how to tell WHICH button has/had focus when Enter was hit so that I can call the click event; without testing for EVERY button on the page and reducing the response time.
Sergey Alexandrovich Kryukov 15-Oct-12 18:08pm    
This is not a problem -- please see my updated answer, after [EDIT].
--SA
Sergey Alexandrovich Kryukov 15-Oct-12 18:09pm    
Yes, I understand that you need to be able to use keyboard only. Isn't it obvious my answer (pay attention for the word "elsewhere" -- are you getting the picture?)
--SA

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