> Am I missing anything?

...the earlier replies in this thread? ;-)

Adapting kangax and Justin's responses to your handleEnter function:

Step 1: Modify your handleEnter function so it accepts an event
argument and calls its stop() function if it handles the keypress:

    function handleEnter(evt)
    {
        if (/* whatever your criteria are for handling the event*/)
        {
            // Handle it:
            /* ... */

            // And prevent the browser from handling it:
            evt.stop();
        }
    }

Then hook it up like this on load:

    $$('input[type=text]').invoke('observe', 'keypress',
handleEnter);

References:
$$(): http://prototypejs.org/api/utility/dollar-dollar
Enumerable.invoke: http://prototypejs.org/api/enumerable/invoke
Element.observe: http://prototypejs.org/api/element/observe
Event.stop: http://prototypejs.org/api/event/stop

HTH,
--
T.J. Crowder
tj / crowder software / com

On Oct 14, 2:06 am, kcnu <[EMAIL PROTECTED]> wrote:
> Thank you for your quick response.
>
> I believe    inputs[i].onkeypress = function(e) {
>   e = e || window.event;
>    ...
>   }
>
> is similar to
>
> inputs[i].onkeypress = "javascript: return handleEnter(this, event)";
> -- this is what I tried and it didn't work either.
>
> Basically what I need to do is, set handleEnter() for all text fields
> at run time to avoid hard coding onkeypress = "javascript: return
> handleEnter(this, event);" for textboxes across pages and handle Enter
> key event to submit the page for those text boxes.
>
> Am I missing anything?
>
> Thanks,
> Kcnu
>
> On Oct 13, 5:22 pm, kangax <[EMAIL PROTECTED]> wrote:
>
> > On Oct 13, 4:52 pm, kcnu <[EMAIL PROTECTED]> wrote:
>
> > > Hello All,
>
> > > I need some help to add 'onkeypress' event attribute to HTML <INPUT>
> > > elements.
>
> > There's a bit easier solution:
>
> > $$('input[type=text]').invoke('observe', 'keypress', function(e) {
> >   if (!test()) e.stop();
>
> > });
>
> > > Below code works fine in Firefox, but not at all in IE 7.
>
> > > var inputs = document.getElementsByTagName("input");
> > >         for (i = 0; i < inputs.length; i++ )
> > >         {
> > >                 if(inputs[i].type == 'text'') {
> > >                 alert('name is-->>>'+inputs[i].name);
> > >                         inputs[i].setAttribute("onkeypress", 
> > > "javascript:return test()");
> > >                 }
> > >         }
>
> > > Tried writeAttribute() in prototype 1.6.0.2 too but no use in IE 7.
>
> > But if you don't feel like using `observe`, just assign a function
> > reference, rather than setting attribute:
>
> > inputs[i].onkeypress = function(e) {
> >   e = e || window.event;
> >   ...
>
> > }
>
> > > Thanks,
> > > Kcnu
>
> > --
> > kangax
>
>
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Prototype & script.aculo.us" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/prototype-scriptaculous?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to