Todd Menier wrote:
> 
> Hello,
> I'm writing a function in a global script that will apply focus to the
> first
> visible enabled form field on a page.  ...
> $('#mainContent
> :input:visible:not(:checkbox):not(:button):not(:submit):not(:image):not([EMAIL
>  PROTECTED]):first')
> 
> I had assumed that the ":first" qualifier would cause the search operation
> to stop after
> it finds a match, but that doesn't seem to be the case.  ... "*:first" is
> equally slow.
> 

With that many elements I suspect it's just the sheer number and the time it
takes to grab them.

There are only a few selectors and operators that would benefit from a
short-circuit operation but I agree this is one place where it would be
useful. Right now, the selectors basically do a breadth-first search of the
DOM tree. By the time it gets to :first it's already selected all the nodes.
I can't see an easy way to add support for a fast :first operator but maybe
someone else can.

So, there has to be another way to make the problem go away. My first
suggestion is that you really don't want a select with hundreds of options.
It will take a long time to load, jQuery or not. Perhaps you could use an
ajax filler for them or some other way to select a subset. 

Okay, so you probably are going to ignore my first suggestion. To make the
selector faster I would recommend trying to process fewer elements. The way
you have it now you're starting with all elements and throwing away the ones
you don't want. (There is an implicit '*' in front of :input.) Try this
instead, which will use getElementsByTagName (did I get all the desired
focusables?)

$('textarea,select,[EMAIL PROTECTED]').select(':not([EMAIL PROTECTED]):first')

I hope that would be a manageable list of elements because it won't have to
look at the options on the Selects That Never End. 

Still too slow? Does the list of potentially focusable form elements change
after the page is loaded? If not, try putting this in your .ready handler;
you could do it on a setTimeout so the page will be usable while it runs.

$focusables = $('textarea,select,[EMAIL PROTECTED]');

Then when it's time to set focus you can just do this:

$(':not([EMAIL PROTECTED]):first', $focusables)[0].focus();

(Assuming you can always guarantee that there is at least one focusable
element left...with a form that big it should be like shooting fish in a
barrel.)

-- 
View this message in context: 
http://www.nabble.com/performance-issues-in-IE-tf2857155.html#a7984639
Sent from the JQuery mailing list archive at Nabble.com.


_______________________________________________
jQuery mailing list
[email protected]
http://jquery.com/discuss/

Reply via email to