If the input doesn't exist, then wouldn't nothing be returned with $
('input').parent().children(':last')  either? It seems to presume the
existence of the input :)

Maybe you meant if the input doesn't have siblings, then nothing is
returned, which is true using nextAll(). Using 'input' with no extra
identifier also means that this would grab all inputs on the page,
which may not be desirable. If the input has an identifier, then you
could also do something like this:

$('input#foo').nextAll(':last').add('input#foo:only-child');

or from your example markup you could do:

$('div input').nextAll(':last').add('div input:only-child'); //fnb it
would grab all inputs in all divs

This will get the last sibling if there is one, and add the input if
it's an only child. Btw, lest somebody think otherwise, I'm not
opposed to traversing up to the parent at all, I just wanted to
continue pursuing the idea without it for fun :)

Rereading the original post makes me question where the emphasis is
"The span might or might not be there, so I would like to just get the
last sibling in a parent". If you're coming from the perspective of
the parent and not the input, then just doing:

$('div').children(':last');

is all you'd really need. Starting from the perspective of the input
you have several choices, which is what makes jQuery so much fun:
there's always more than one way to accomplish some task! It's left to
you to figure out what is optimal (overall or in your particular
situation), and that will have to consider speed, legibility for other
coders, isolation from structure, etc.


On Feb 12, 5:30 am, Ricardo Tomasi <ricardob...@gmail.com> wrote:
> If the input doesn`t exist nothing will be returned with nextAll. Same
> with jQuery Lover's approach. That's we need to get all the children:
>
> $('input').parent().children(':last')
>
> or alternatively
>
> $('input').siblings().andSelf().filter(':last-child') // I'd bet the
> other one is faster
>
> As Mike Manning pointed out, $("div :last-child") will return all the
> 'last-child's of all the descendants, not just the children. $("div
>
> > :last-child") would be it.
>
> cheers,
> - ricardo
>
> On Feb 12, 4:19 am, mkmanning <michaell...@gmail.com> wrote:
>
> > Somehow the selector disappeared :P
>
> > $('input').nextAll(':last');
>
> > $(this).nextAll(':last');
>
> > On Feb 11, 10:17 pm, mkmanning <michaell...@gmail.com> wrote:
>
> > > $("div :last-child");  finds all of the last-child elements, including
> > > descendant elements (e.g.  if there were an <a> inside the span in
> > > your example it would be returned too). You can also not worry about
> > > the parent at all and just use the sibling selector:
>
> > >  //or you could use :last-child
>
> > > As Ricardo suggested, you can use 'this' :
>
> > > $(this).nextAll(':last');
>
> > > On Feb 11, 10:05 pm, Ricardo Tomasi <ricardob...@gmail.com> wrote:
>
> > > > $(this).parent().children(':last') //or :last-child - assuming 'this'
> > > > is the input element
>
> > > > On Feb 12, 3:09 am, Risingfish <risingf...@gmail.com> wrote:
>
> > > > > Thanks Nic,
>
> > > > > How about if I'm using the input tag as the base? Can I do something
> > > > > like $("self:parent:last-child") ?
>
> > > > > On Feb 11, 8:13 pm, Nic Luciano <adaptive...@gmail.com> wrote:
>
> > > > > > $("div :last-child");
>
> > > > > > (or this wacky way I tried before I learned CSS selectors....
> > > > > > $("div").children()[$("div").children().size() - 1])...)
>
> > > > > > Nic 
> > > > > > Lucianohttp://www.twitter.com/niclucianohttp://www.linkedin.com/in/nicluciano
>
> > > > > > On Wed, Feb 11, 2009 at 9:49 PM, Risingfish <risingf...@gmail.com> 
> > > > > > wrote:
>
> > > > > > > Hi James, apologize for not being more precise. I'm using the 
> > > > > > > input
> > > > > > > element as my base reference. So using that input element, I want 
> > > > > > > to
> > > > > > > get the last sibling under the div. You are correct that if the 
> > > > > > > span
> > > > > > > is not then the input is what I want. I may need to add more tags
> > > > > > > later (I'm sure you know hoe the development process goes when
> > > > > > > customers are involved. :) ), so a reliable way to get the last 
> > > > > > > tag
> > > > > > > would be nice. Thanks!
>
> > > > > > > On Feb 11, 7:21 pm, James <james.gp....@gmail.com> wrote:
> > > > > > > > When you say "last sibling in a parent", what does that mean? 
> > > > > > > > Which
> > > > > > > > element is your base reference?
>
> > > > > > > > In your example:
> > > > > > > > <div>
> > > > > > > >   <input ...... />
> > > > > > > >   <span>...</span>
> > > > > > > > </div>
>
> > > > > > > > Do you mean your <div> is the base reference, and you want to 
> > > > > > > > find the
> > > > > > > > last child (thus, <span>) relative to that?
> > > > > > > > And if <span> is not there, you want <input>?
>
> > > > > > > > On Feb 11, 4:12 pm, Risingfish <risingf...@gmail.com> wrote:
>
> > > > > > > > > Before I accidentally hit enter, this is what I was trying to 
> > > > > > > > > type: :)
>
> > > > > > > > > <div>
> > > > > > > > >   <input ...... />
> > > > > > > > >   <span>...</span>
> > > > > > > > > </div>
>
> > > > > > > > > The span might or might not be there, so I would like to just 
> > > > > > > > > get the
> > > > > > > > > last sibling in a parent.
>
> > > > > > > > > Thanks!

Reply via email to