Hi Steve,
> Well, I tried rewriting the focus call as:
>
> onComplete: function() {$('editdescription').focus.defer();
>
> Is that the right way to put it?
No, you'll need to use my function or one like it. (The function is
generic, so you can add it to any utility class you have lying around
and reuse it.) You can't use #defer on instance methods, because it
doesn't preserve context (the `this` value within the function). You'd
have to pass it through Function#bind[1] first, and that starts
getting overkill.
You have the right idea, though. For instance, if we had a function
that accepted the element as its first parameter, what you did would
work. Take Element.setStyle[2] for example. Element.setStyle can be
called as a function where you pass it the element or its ID as the
first parameter. So we could do this if we wanted to set the style
after a brief pause:
Element.setStyle.defer('someElement', { ...style info...});
That works because Element.setStyle doesn't care about `this`, it's
not an instance method. But there's no non-method version of
Element#focus, which is why I had to create one -- and I threw in
retries for good measure.
[1] http://api.prototypejs.org/language/function.html#bind-instance_method
[2] http://api.prototypejs.org/dom/element.html#setstyle-class_method
HTH,
--
T.J. Crowder
tj / crowder software / com
www.crowdersoftware.com
On Sep 28, 8:15 am, Steve Marshall <[email protected]> wrote:
> Well, I tried rewriting the focus call as:
>
> onComplete: function() {$('editdescription').focus.defer();
>
> Is that the right way to put it? Please remember I'm very new to all
> this, and still getting my head around it.
> If it is, it didn't work - in fact it stopped Firefox from working.
> This is not by any means a show-stopper, so I have put it back how it
> was and will put up with it until I am a lot more familiar with all
> this.
>
> Thanks
> Steve
>
> On Sep 28, 2:47 pm, Steve Marshall <[email protected]> wrote:
>
>
>
> > Thanks for the very quick replies. I've discovered in the meantime
> > that it actually DOES work perfectly fine on Firefox - just not on
> > IE7. These are the two browsers I normally test stuff on. So if it's
> > me, it's only partly me ;-)
>
> > I'll try the defer. Thanks again.
>
> > Cheers
> > Steve
>
> > On Sep 28, 2:34 pm, "T.J. Crowder" <[email protected]> wrote:
>
> > > Hi Steve, DJ,
>
> > > In my experience usually it *is* there by the time onComplete is
> > > called, but just in case it is a matter of the browser not having
> > > caught it's breath, Steve you can use Function#defer[1] to give the
> > > browser a moment. It may be overkill, but you could even encapsulate
> > > this into a function:
>
> > > function focusWithRetry(id, attempts) {
> > > var element;
>
> > > element = $(id);
> > > if (element) {
> > > element.focus();
> > > }
> > > else if (--attempts > 0) {
> > > focusWithRetry.defer(id, attempts);
> > > }
>
> > > }
>
> > > But I wonder if it may be something else. If #defer doesn't do it, can
> > > you create a minimal failing test case?[2] You may figure it out by
> > > doing that, or if not it'll give folks on the list something to get
> > > mano-a-mano with.
>
> > > [1]http://api.prototypejs.org/language/function.html#defer-instance_method
> > > [2]http://proto-scripty.wikidot.com/self-contained-test-page
>
> > > HTH,
> > > --
> > > T.J. Crowder
> > > tj / crowder software / comwww.crowdersoftware.com
>
> > > On Sep 28, 7:08 am, DJ Mangus <[email protected]> wrote:
>
> > > > If the id 'editdescription' is added to the DOM it won't have been
> > > > processed by the browser OnComplete. You will have to delay the
> > > > function.
>
> > > > Sent from my phone so pardon the spelling errors.
>
> > > > On Sep 27, 2009, at 10:59 PM, Steve Marshall <[email protected]>
> > > > wrote:
>
> > > > > Hi all. I'm brand-new to Prototype - just discovered it yesterday,
> > > > > and I must say it feels like I have come home. It seems to be at just
> > > > > the right level, and it just works (so far). I'm a happy camper!
>
> > > > > One minor thing - I tried using an Ajax.Updater, and when it completes
> > > > > I want to set the focus to an entry field, but it isn't working.
> > > > > Here's my Ajax.Updater:
>
> > > > > new Ajax.Updater('row'+code, 'svc/ajax.php', {
> > > > > parameters: {action: 'editlibcat', code: catcode},
> > > > > onComplete: function() {$('editdescription').focus();}
> > > > > });
>
> > > > > This call sets a row in a table into editable form, and there's
> > > > > another Updater that does the update - pretty standard stuff I'd
> > > > > reckon. There definitely IS an element with ID='editdescription' -
> > > > > it's in the HTML that is returned from the Ajax call. But focus never
> > > > > gets set. Everything else is perfect. It must be me - what do I have
> > > > > wrong?
>
> > > > > Thanks again for Prototype - it's great!
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---