Prototype extension of HTMLFormElement broken?

2 views
Skip to first unread message

Michael Schuerig

unread,
Jan 19, 2007, 7:56:47 AM1/19/07
to rubyonrail...@googlegroups.com

Supposedly Prototype extends the HTMLFormElement.prototype so that the
following code ought to work

$('myform').focusFirstElement()

Strangely, it does not work in Firefox 2.0. It complains that
focusFirstElement is not a function. Firebug tells me that
HTMLFormElement.prototype.focusFirstElement indeed is a function. I
can't say I understand what's happening here.

Michael

--
Michael Schuerig
mailto:mic...@schuerig.de
http://www.schuerig.de/michael/

Michael Schuerig

unread,
Jan 20, 2007, 7:09:19 AM1/20/07
to rubyonrail...@googlegroups.com
On Friday 19 January 2007 13:56, Michael Schuerig wrote:
> Supposedly Prototype extends the HTMLFormElement.prototype so that
> the following code ought to work
>
> $('myform').focusFirstElement()
>
> Strangely, it does not work in Firefox 2.0. It complains that
> focusFirstElement is not a function.

Well, select is not broken and neither is focusFirstElement().
Unfortunately, in my concrete case it is. I'll have to find out and
when it's something interesting or very dumb on my part, I'll follow
up.

RobG

unread,
Jan 20, 2007, 10:20:18 AM1/20/07
to Ruby on Rails: Spinoffs

Michael Schuerig wrote:
> Supposedly Prototype extends the HTMLFormElement.prototype so that the
> following code ought to work
>
> $('myform').focusFirstElement()

The function in Prototype.js should provide a hint:

focusFirstElement: function(form) { ... }


Which indicates it requires a form as an argument. Try:

focusFirstElement('myform');


Which works in Firefox but fails in Safari. You might prefer the much
more widely supported:

document.forms['myform'].elements[0].focus();


or, if the form's name is myform you can use the shorter:

document.myform.elements[0].focus();


--
Rob

Michael Schuerig

unread,
Jan 20, 2007, 12:46:18 PM1/20/07
to rubyonrail...@googlegroups.com
On Saturday 20 January 2007 16:20, RobG wrote:
> Michael Schuerig wrote:
> > Supposedly Prototype extends the HTMLFormElement.prototype so that
> > the following code ought to work
> >
> > $('myform').focusFirstElement()
>
> The function in Prototype.js should provide a hint:
>
> focusFirstElement: function(form) { ... }
>
>
> Which indicates it requires a form as an argument. Try:
>
> focusFirstElement('myform');

Nope. The functions in Form.Methods are mixed into
HTMLFormElement.prototype, where possible, otherwise they are added to
each form accessed through $('myform'). Therefore

$('myform').focusFirstElement()

does indeed work. That it didn't work for me originally was due to some
braindeadness on my part. Let's say when 'myform' is really the id of a
form element, then it does work at least in Firefox, Konqueror, and
IE7.

Reply all
Reply to author
Forward
0 new messages