A jQuery object is an array of 1 or more objects. I'm going to
simplify things down a little.
$('#B, #A')[0].focus();
The selector "#B, #A" tries to find objects with ids of "B" and "A"
(in that order). If "B" is not present in the document then it's just
not in the list, so "A" is the first element in the array. If "B" is
present in the document then it is the first element in the array.
The [0] is just a "shortcut" to get the first element in the array.
Note that you get back a DOM object, not a jQuery object. If you want
a jQuery object then use $(...).eq(0).
You then call the DOM object's focus() function.
Hope that makes things clear.
Karl Rudd
On 1/16/07, Gerry Danen <[EMAIL PROTECTED]> wrote:
> That works too, Karl.
>
> Can you explain how it works?
>
> An interesting thing I just noticed in IE7 is that on
> http://test.danen.org/secure/register.htm the first, second, and
> fourth fields are yellow. Bizarre. If this list allows attachments, I
> can share a partial screenshot. Otherwise check the url.
>
> IE drives me to drinking. :(
>
> Gerry
>
> On 1/15/07, Karl Rudd <[EMAIL PROTECTED]> wrote:
> > Or try
> >
> > $(function() {
> > $('#second_field_to_enter,#first_field_to_enter')[0].focus();
> > });
> >
> > Karl Rudd
> >
> > On 1/16/07, Gerry Danen <[EMAIL PROTECTED]> wrote:
> > > In this piece of code (common to a number of pages) I want to set
> > > focus to a second input field if there is one.
> > >
> > > $(function() {
> > > $("#first_field_to_enter")[0].focus();
> > > $("#second_field_to_enter")[0].focus();
> > > });
> > >
> > > If the second input field is present, focus is on the second field. If
> > > not present, focus goes to the first field. All good, except that an
> > > error is generated in FF2 error console when the second field is not
> > > present.
> > >
> > > See http://test.danen.org/secure/register.htm and
> > > http://test.danen.org/secure/ for an example of each case.
> > >
> > > html is:
> > > <input id="first_field_to_enter" type="text" name="form_id" size="26">
> > > <input id="second_field_to_enter" type=text name=rf_namef size="50"
> > > maxlength="50">
> > >
> > > Both fields are in different <form>s.
> > >
> > > Is there a way to test for the presence of the second field?
> > >
> > > Please excuse my ignorance if the answer is obvious, but I'm a
> > > JavaScript newbie, and I just found jQuery today. John Resig and the
> > > jQuery team, hats off to your excellent work.
> > >
>
> _______________________________________________
> jQuery mailing list
> [email protected]
> http://jquery.com/discuss/
>
_______________________________________________
jQuery mailing list
[email protected]
http://jquery.com/discuss/