Forgot to mention that if you want to fire it on all elements in the
form, then you could (once again not tested):
<script>
for (var f = 0; f < document.forms.length; f++)
{
var form = document.forms[f]
for (var e = 0; e < form.elements.length; e++)
{
var elem = form.elements[e]
if (elem.name != 'input') continue // add more controls to support here
if (elem.type && elem.type == 'text')
elem.onkeyup = submitform
}
}
function submitform(event)
{
var keycode
if (window.event)
keycode = window.event.keycode
else
keycode = event.keycode
if (keycode != 13) return
var fireOnThis = document.getElementById('defaultSubmitElement')
if (document.createEvent)
{
var evObj = document.createEvent('MouseEvents')
evObj.initEvent( 'click', true, false )
fireOnThis.dispatchEvent(evObj)
}
else if (document.createEventObject)
{
fireOnThis.fireEvent('onclick')
}
}
</script>
<t:commandButton id="defaultSubmitElement" forceId="true" />
This isn't as nice as the same button/link has to be used for each
form and element, but you don't have to put the handler on each
element either. Up to you.
-Andrew
On 4/21/06, Andrew Robinson <[EMAIL PROTECTED]> wrote:
> You could add a javascript function that catches the key up event and
> add it to each control passing the ID of the
> commandlink/commandbutton, and force the click event of that
> link/button.
>
> Example (didn't test it, but should work for IE and firefox):
> <t:outputText onkeyup="checkDefaultSubmit(event, 'mysubmit')" />
> <t:commandLink id="mysubmit" forceId="true" value="submit" />
>
> Javascript:
> function checkDefaultSubmit(event, controlId)
> {
> if (event.keycode != 13) return
> var fireOnThis = document.getElementById(controlId)
> if (document.createEvent)
> {
> var evObj = document.createEvent('MouseEvents')
> evObj.initEvent( 'click', true, false )
> fireOnThis.dispatchEvent(evObj)
> }
> else if (document.createEventObject)
> {
> fireOnThis.fireEvent('onclick')
> }
> }
>
>
>
> On 4/21/06, Lindholm, Greg <[EMAIL PROTECTED]> wrote:
> > Is there anything in MyFaces/Tomahawk for specifing the
> > default action to take when the user presses Enter key
> > in a form?
> >
> > How are people dealing with this?
> >
> > I know there is a j4j:defaultAction but it doesn't work
> > for Firefox.
> > (http://www.jsftutorials.net/defaultActionTag.html)
> > Has anyone built a version that works with Firefox?
> >
> > Greg
> >
> >
>