[jQuery] Re: Is A Child Of?

2009-02-12 Thread Ami

Thank you.
It's was help me a lot.

I am writing a small tooltip plugin, and I use it to now when  the
user click on an element inside the tooltip

Thank you again.

On 12 פברואר, 00:05, mkmanning  wrote:
> I just wrote this in response to this thread and haven't checked it
> thoroughly (other than my head, which has been known to be buggy).
> I prefer to be able to pass in a jQuery object as the parent:
>
> jQuery.fn.childOf = function(a){
>         return (this.length === this.map(function(){if($.inArray
> (this,a.children())!=-1){return this;}}).length);
>
> };
>
> Test in Firebug:
> 
>         
>                 one
>                 two
>                 three
>                 four
>         
> 
> console.log( $('#kid1, #kid2, #kid3').childOf( $('#gramps') ) );        //
> logs false
> console.log( $('#kid1, #kid2, #kid3').childOf( $('#pops') ) ); //logs
> true
> console.log( $('#kid1, #kid2, #kid3, #subkid3').childOf( $
> ('#pops') ) ); //logs false
> console.log( $('#pops').childOf( $('#gramps') ) );      //logs true
> console.log( $('#pops').childOf( $('#pops') ) );         //logs false
>
> On Feb 11, 1:45 pm, Ricardo Tomasi  wrote:
>
>
>
> > 'return true' inside the each callback works like a 'continue',
> > skipping to next iteration. The value is never returned to the outside
> > scope (would break chaining).
>
> > cheers,
> > - ricardo
>
> > On Feb 11, 6:33 pm, Ami  wrote:
>
> > > You can shave a few ms, and memory:
> > > ***
> > > jQuery.fn.childOf = function(a){
> > >    this.each(function(){
> > >       if (this.parentNode == a) return true;
> > >        });
> > >    return false;});
>
> > > ***
>
> > > On Feb 11, 8:18 pm, Ricardo Tomasi  wrote:
>
> > > > You can shave a few ms off that by using the current object itself,
> > > > and cacheing the other element:
>
> > > > jQuery.fn.in = function(a){
> > > >   var a = $(a)[0];
> > > >   return !!this.parents().filter(function(){ return this ===
> > > > a;}).length;
>
> > > > };
>
> > > > Also be aware that this actually checks if the element is a
> > > > *descendant* of the other, not just achild. For a simple (and faster)
> > > > 'childOf' check use this:
>
> > > > jQuery.fn.childOf = function(a){
> > > >   var p = false;
> > > >   this.each(function(){
> > > >       if (this.parentNode == a) p = true;
> > > >    });
> > > >    return p;
>
> > > > };
>
> > > > cheers,
> > > > - ricardo
>
> > > > On Feb 11, 1:56 pm, Ami  wrote:
>
> > > > > The Best solution:
>
> > > > > I have writed a very small plugin (1 line) for that, by using the code
> > > > > of Richard
>
> > > > > jQuery.fn.childOf=function(a){var b=this;return ($(b).parents().filter
> > > > > (function() { return this === $(a)[0]; }).length )}
>
> > > > > For Example:
> > > > > $("div").childOf(document.body)
> > > > > $("div").childOf($(document.body))
>
> > > > > I hope that there is  no bugs in this code.
>
> > > > > On 22 ינואר, 11:25, "James Hughes"  wrote:
>
> > > > > > Hi,
>
> > > > > > This is probably a really easy question an I apologise if it appear 
> > > > > > stupid but I am clueless right now.  Given 2 jQuery objects (a,b) 
> > > > > > how can I tell if b is achildof a?
>
> > > > > > James
>
> > > > > > 
> > > > > > This e-mail is intended solely for the addressee and is strictly 
> > > > > > confidential; if you are not the addressee please destroy the 
> > > > > > message and all copies. Any opinion or information contained in 
> > > > > > this email or its attachments that does not relate to the business 
> > > > > > of Kainos
> > > > > > is personal to the sender and is not given by or endorsed by 
> > > > > > Kainos. Kainos is the trading name of Kainos Software Limited, 
> > > > > > registered in Northern Ireland under company number: NI19370, 
> > > > > > having its registered offices at: Kainos House, 4-6 Upper Crescent, 
> > > > > > Belfast, BT7 1NT,
> > > > > > Northern Ireland. Registered in the UK for VAT under number: 
> > > > > > 454598802 and registered in Ireland for VAT under number: 9950340E. 
> > > > > > This email has been scanned for all known viruses by MessageLabs 
> > > > > > but is not guaranteed to be virus free; further terms and 
> > > > > > conditions may be
> > > > > > found on our website -www.kainos.com


[jQuery] Re: Is A Child Of?

2009-02-11 Thread mkmanning

I just wrote this in response to this thread and haven't checked it
thoroughly (other than my head, which has been known to be buggy).
I prefer to be able to pass in a jQuery object as the parent:

jQuery.fn.childOf = function(a){
return (this.length === this.map(function(){if($.inArray
(this,a.children())!=-1){return this;}}).length);
};

Test in Firebug:


one
two
three
four


console.log( $('#kid1, #kid2, #kid3').childOf( $('#gramps') ) );//
logs false
console.log( $('#kid1, #kid2, #kid3').childOf( $('#pops') ) ); //logs
true
console.log( $('#kid1, #kid2, #kid3, #subkid3').childOf( $
('#pops') ) ); //logs false
console.log( $('#pops').childOf( $('#gramps') ) );  //logs true
console.log( $('#pops').childOf( $('#pops') ) ); //logs false


On Feb 11, 1:45 pm, Ricardo Tomasi  wrote:
> 'return true' inside the each callback works like a 'continue',
> skipping to next iteration. The value is never returned to the outside
> scope (would break chaining).
>
> cheers,
> - ricardo
>
> On Feb 11, 6:33 pm, Ami  wrote:
>
> > You can shave a few ms, and memory:
> > ***
> > jQuery.fn.childOf = function(a){
> >    this.each(function(){
> >       if (this.parentNode == a) return true;
> >        });
> >    return false;});
>
> > ***
>
> > On Feb 11, 8:18 pm, Ricardo Tomasi  wrote:
>
> > > You can shave a few ms off that by using the current object itself,
> > > and cacheing the other element:
>
> > > jQuery.fn.in = function(a){
> > >   var a = $(a)[0];
> > >   return !!this.parents().filter(function(){ return this ===
> > > a;}).length;
>
> > > };
>
> > > Also be aware that this actually checks if the element is a
> > > *descendant* of the other, not just achild. For a simple (and faster)
> > > 'childOf' check use this:
>
> > > jQuery.fn.childOf = function(a){
> > >   var p = false;
> > >   this.each(function(){
> > >       if (this.parentNode == a) p = true;
> > >    });
> > >    return p;
>
> > > };
>
> > > cheers,
> > > - ricardo
>
> > > On Feb 11, 1:56 pm, Ami  wrote:
>
> > > > The Best solution:
>
> > > > I have writed a very small plugin (1 line) for that, by using the code
> > > > of Richard
>
> > > > jQuery.fn.childOf=function(a){var b=this;return ($(b).parents().filter
> > > > (function() { return this === $(a)[0]; }).length )}
>
> > > > For Example:
> > > > $("div").childOf(document.body)
> > > > $("div").childOf($(document.body))
>
> > > > I hope that there is  no bugs in this code.
>
> > > > On 22 ינואר, 11:25, "James Hughes"  wrote:
>
> > > > > Hi,
>
> > > > > This is probably a really easy question an I apologise if it appear 
> > > > > stupid but I am clueless right now.  Given 2 jQuery objects (a,b) how 
> > > > > can I tell if b is achildof a?
>
> > > > > James
>
> > > > > 
> > > > > This e-mail is intended solely for the addressee and is strictly 
> > > > > confidential; if you are not the addressee please destroy the message 
> > > > > and all copies. Any opinion or information contained in this email or 
> > > > > its attachments that does not relate to the business of Kainos
> > > > > is personal to the sender and is not given by or endorsed by Kainos. 
> > > > > Kainos is the trading name of Kainos Software Limited, registered in 
> > > > > Northern Ireland under company number: NI19370, having its registered 
> > > > > offices at: Kainos House, 4-6 Upper Crescent, Belfast, BT7 1NT,
> > > > > Northern Ireland. Registered in the UK for VAT under number: 
> > > > > 454598802 and registered in Ireland for VAT under number: 9950340E. 
> > > > > This email has been scanned for all known viruses by MessageLabs but 
> > > > > is not guaranteed to be virus free; further terms and conditions may 
> > > > > be
> > > > > found on our website -www.kainos.com


[jQuery] Re: Is A Child Of?

2009-02-11 Thread Ricardo Tomasi

'return true' inside the each callback works like a 'continue',
skipping to next iteration. The value is never returned to the outside
scope (would break chaining).

cheers,
- ricardo

On Feb 11, 6:33 pm, Ami  wrote:
> You can shave a few ms, and memory:
> ***
> jQuery.fn.childOf = function(a){
>    this.each(function(){
>       if (this.parentNode == a) return true;
>        });
>    return false;});
>
> ***
>
> On Feb 11, 8:18 pm, Ricardo Tomasi  wrote:
>
> > You can shave a few ms off that by using the current object itself,
> > and cacheing the other element:
>
> > jQuery.fn.in = function(a){
> >   var a = $(a)[0];
> >   return !!this.parents().filter(function(){ return this ===
> > a;}).length;
>
> > };
>
> > Also be aware that this actually checks if the element is a
> > *descendant* of the other, not just achild. For a simple (and faster)
> > 'childOf' check use this:
>
> > jQuery.fn.childOf = function(a){
> >   var p = false;
> >   this.each(function(){
> >       if (this.parentNode == a) p = true;
> >    });
> >    return p;
>
> > };
>
> > cheers,
> > - ricardo
>
> > On Feb 11, 1:56 pm, Ami  wrote:
>
> > > The Best solution:
>
> > > I have writed a very small plugin (1 line) for that, by using the code
> > > of Richard
>
> > > jQuery.fn.childOf=function(a){var b=this;return ($(b).parents().filter
> > > (function() { return this === $(a)[0]; }).length )}
>
> > > For Example:
> > > $("div").childOf(document.body)
> > > $("div").childOf($(document.body))
>
> > > I hope that there is  no bugs in this code.
>
> > > On 22 ינואר, 11:25, "James Hughes"  wrote:
>
> > > > Hi,
>
> > > > This is probably a really easy question an I apologise if it appear 
> > > > stupid but I am clueless right now.  Given 2 jQuery objects (a,b) how 
> > > > can I tell if b is achildof a?
>
> > > > James
>
> > > > 
> > > > This e-mail is intended solely for the addressee and is strictly 
> > > > confidential; if you are not the addressee please destroy the message 
> > > > and all copies. Any opinion or information contained in this email or 
> > > > its attachments that does not relate to the business of Kainos
> > > > is personal to the sender and is not given by or endorsed by Kainos. 
> > > > Kainos is the trading name of Kainos Software Limited, registered in 
> > > > Northern Ireland under company number: NI19370, having its registered 
> > > > offices at: Kainos House, 4-6 Upper Crescent, Belfast, BT7 1NT,
> > > > Northern Ireland. Registered in the UK for VAT under number: 454598802 
> > > > and registered in Ireland for VAT under number: 9950340E. This email 
> > > > has been scanned for all known viruses by MessageLabs but is not 
> > > > guaranteed to be virus free; further terms and conditions may be
> > > > found on our website -www.kainos.com


[jQuery] Re: Is A Child Of?

2009-02-11 Thread Ami

You can shave a few ms, and memory:
***
jQuery.fn.childOf = function(a){
   this.each(function(){
  if (this.parentNode == a) return true;
   });
   return false;
});
***

On Feb 11, 8:18 pm, Ricardo Tomasi  wrote:
> You can shave a few ms off that by using the current object itself,
> and cacheing the other element:
>
> jQuery.fn.in = function(a){
>   var a = $(a)[0];
>   return !!this.parents().filter(function(){ return this ===
> a;}).length;
>
> };
>
> Also be aware that this actually checks if the element is a
> *descendant* of the other, not just achild. For a simple (and faster)
> 'childOf' check use this:
>
> jQuery.fn.childOf = function(a){
>   var p = false;
>   this.each(function(){
>       if (this.parentNode == a) p = true;
>    });
>    return p;
>
> };
>
> cheers,
> - ricardo
>
> On Feb 11, 1:56 pm, Ami  wrote:
>
> > The Best solution:
>
> > I have writed a very small plugin (1 line) for that, by using the code
> > of Richard
>
> > jQuery.fn.childOf=function(a){var b=this;return ($(b).parents().filter
> > (function() { return this === $(a)[0]; }).length )}
>
> > For Example:
> > $("div").childOf(document.body)
> > $("div").childOf($(document.body))
>
> > I hope that there is  no bugs in this code.
>
> > On 22 ינואר, 11:25, "James Hughes"  wrote:
>
> > > Hi,
>
> > > This is probably a really easy question an I apologise if it appear 
> > > stupid but I am clueless right now.  Given 2 jQuery objects (a,b) how can 
> > > I tell if b is achildof a?
>
> > > James
>
> > > 
> > > This e-mail is intended solely for the addressee and is strictly 
> > > confidential; if you are not the addressee please destroy the message and 
> > > all copies. Any opinion or information contained in this email or its 
> > > attachments that does not relate to the business of Kainos
> > > is personal to the sender and is not given by or endorsed by Kainos. 
> > > Kainos is the trading name of Kainos Software Limited, registered in 
> > > Northern Ireland under company number: NI19370, having its registered 
> > > offices at: Kainos House, 4-6 Upper Crescent, Belfast, BT7 1NT,
> > > Northern Ireland. Registered in the UK for VAT under number: 454598802 
> > > and registered in Ireland for VAT under number: 9950340E. This email has 
> > > been scanned for all known viruses by MessageLabs but is not guaranteed 
> > > to be virus free; further terms and conditions may be
> > > found on our website -www.kainos.com


[jQuery] Re: Is A Child Of?

2009-02-11 Thread Ricardo Tomasi

You can shave a few ms off that by using the current object itself,
and cacheing the other element:

jQuery.fn.in = function(a){
  var a = $(a)[0];
  return !!this.parents().filter(function(){ return this ===
a;}).length;
};

Also be aware that this actually checks if the element is a
*descendant* of the other, not just a child. For a simple (and faster)
'childOf' check use this:

jQuery.fn.childOf = function(a){
  var p = false;
  this.each(function(){
  if (this.parentNode == a) p = true;
   });
   return p;
};

cheers,
- ricardo

On Feb 11, 1:56 pm, Ami  wrote:
> The Best solution:
>
> I have writed a very small plugin (1 line) for that, by using the code
> of Richard
>
> jQuery.fn.childOf=function(a){var b=this;return ($(b).parents().filter
> (function() { return this === $(a)[0]; }).length )}
>
> For Example:
> $("div").childOf(document.body)
> $("div").childOf($(document.body))
>
> I hope that there is  no bugs in this code.
>
> On 22 ינואר, 11:25, "James Hughes"  wrote:
>
> > Hi,
>
> > This is probably a really easy question an I apologise if it appear stupid 
> > but I am clueless right now.  Given 2 jQuery objects (a,b) how can I tell 
> > if b is a child of a?
>
> > James
>
> > 
> > This e-mail is intended solely for the addressee and is strictly 
> > confidential; if you are not the addressee please destroy the message and 
> > all copies. Any opinion or information contained in this email or its 
> > attachments that does not relate to the business of Kainos
> > is personal to the sender and is not given by or endorsed by Kainos. Kainos 
> > is the trading name of Kainos Software Limited, registered in Northern 
> > Ireland under company number: NI19370, having its registered offices at: 
> > Kainos House, 4-6 Upper Crescent, Belfast, BT7 1NT,
> > Northern Ireland. Registered in the UK for VAT under number: 454598802 and 
> > registered in Ireland for VAT under number: 9950340E. This email has been 
> > scanned for all known viruses by MessageLabs but is not guaranteed to be 
> > virus free; further terms and conditions may be
> > found on our website -www.kainos.com


[jQuery] Re: Is A Child Of?

2009-02-11 Thread Ami

The Best solution:

I have writed a very small plugin (1 line) for that, by using the code
of Richard

jQuery.fn.childOf=function(a){var b=this;return ($(b).parents().filter
(function() { return this === $(a)[0]; }).length )}

For Example:
$("div").childOf(document.body)
$("div").childOf($(document.body))

I hope that there is  no bugs in this code.


On 22 ינואר, 11:25, "James Hughes"  wrote:
> Hi,
>
> This is probably a really easy question an I apologise if it appear stupid 
> but I am clueless right now.  Given 2 jQuery objects (a,b) how can I tell if 
> b is a child of a?
>
> James
>
> 
> This e-mail is intended solely for the addressee and is strictly 
> confidential; if you are not the addressee please destroy the message and all 
> copies. Any opinion or information contained in this email or its attachments 
> that does not relate to the business of Kainos
> is personal to the sender and is not given by or endorsed by Kainos. Kainos 
> is the trading name of Kainos Software Limited, registered in Northern 
> Ireland under company number: NI19370, having its registered offices at: 
> Kainos House, 4-6 Upper Crescent, Belfast, BT7 1NT,
> Northern Ireland. Registered in the UK for VAT under number: 454598802 and 
> registered in Ireland for VAT under number: 9950340E. This email has been 
> scanned for all known viruses by MessageLabs but is not guaranteed to be 
> virus free; further terms and conditions may be
> found on our website -www.kainos.com


[jQuery] Re: Is A Child Of?

2009-01-22 Thread Karl Swedberg
Do you have to start with two jQuery objects? It would be a lot easier  
if you could start with two simple selectors. For example, given  
div.outer and span.inner, to see if span.inner is a descendant of  
div.outer, you could go from the other direction:


$('div.outer:has(span.inner)').length

If you realy need it to be a child, and not any descendant, you could do

$('div.outer:has(> span.inner)').length


--Karl


Karl Swedberg
www.englishrules.com
www.learningjquery.com




On Jan 22, 2009, at 4:39 AM, Richard D. Worth wrote:


My first thought was to try these and neither worked

$(b, a).length

a.find(b).length

Also, no luck here

$(b[0], a[0]).length

a.find(b[0]).length

In the end this is the one I could get to work

$(b).parents().filter(function() { return this === a[0]; }).length

- Richard

On Thu, Jan 22, 2009 at 4:25 AM, James Hughes   
wrote:



Hi,

This is probably a really easy question an I apologise if it appear  
stupid but I am clueless right now.  Given 2 jQuery objects (a,b)  
how can I tell if b is a child of a?


James


This e-mail is intended solely for the addressee and is strictly  
confidential; if you are not the addressee please destroy the  
message and all copies. Any opinion or information contained in this  
email or its attachments that does not relate to the business of  
Kainos
is personal to the sender and is not given by or endorsed by Kainos.  
Kainos is the trading name of Kainos Software Limited, registered in  
Northern Ireland under company number: NI19370, having its  
registered offices at: Kainos House, 4-6 Upper Crescent, Belfast,  
BT7 1NT,
Northern Ireland. Registered in the UK for VAT under number:  
454598802 and registered in Ireland for VAT under number: 9950340E.  
This email has been scanned for all known viruses by MessageLabs but  
is not guaranteed to be virus free; further terms and conditions may  
be

found on our website - www.kainos.com







[jQuery] Re: Is A Child Of?

2009-01-22 Thread Ricardo Tomasi

I've coded a plugin for that:

http://ff6600.org/j/jquery.childOf.js

You can also do this, but it's much slower:

var a = $(a), b = $(b);

b.parents().filter(function(){
  return this == a[0];
}).length

cheers,
- ricardo

On Jan 22, 7:25 am, "James Hughes"  wrote:
> Hi,
>
> This is probably a really easy question an I apologise if it appear stupid 
> but I am clueless right now.  Given 2 jQuery objects (a,b) how can I tell if 
> b is a child of a?
>
> James
>
> 
> This e-mail is intended solely for the addressee and is strictly 
> confidential; if you are not the addressee please destroy the message and all 
> copies. Any opinion or information contained in this email or its attachments 
> that does not relate to the business of Kainos
> is personal to the sender and is not given by or endorsed by Kainos. Kainos 
> is the trading name of Kainos Software Limited, registered in Northern 
> Ireland under company number: NI19370, having its registered offices at: 
> Kainos House, 4-6 Upper Crescent, Belfast, BT7 1NT,
> Northern Ireland. Registered in the UK for VAT under number: 454598802 and 
> registered in Ireland for VAT under number: 9950340E. This email has been 
> scanned for all known viruses by MessageLabs but is not guaranteed to be 
> virus free; further terms and conditions may be
> found on our website -www.kainos.com


[jQuery] Re: Is A Child Of?

2009-01-22 Thread James Hughes

Excellent.  Funny it seems like such a common task when you think about it 
though I've never needed it before and given the verbosity of the solution it 
seems not many other people have need it either :-P
 
Anyways thanks again you saved my sanity.
 
James.



From: jquery-en@googlegroups.com on behalf of Richard D. Worth
Sent: Thu 22/01/2009 09:39
To: jquery-en@googlegroups.com
Subject: [jQuery] Re: Is A Child Of?


My first thought was to try these and neither worked

$(b, a).length

a.find(b).length

Also, no luck here

$(b[0], a[0]).length

a.find(b[0]).length

In the end this is the one I could get to work

$(b).parents().filter(function() { return this === a[0]; }).length

- Richard


On Thu, Jan 22, 2009 at 4:25 AM, James Hughes  wrote:




Hi,

This is probably a really easy question an I apologise if it appear 
stupid but I am clueless right now.  Given 2 jQuery objects (a,b) how can I 
tell if b is a child of a?

James <http://www.kainos.com/> 







This e-mail is intended solely for the addressee and is strictly confidential; 
if you are not the addressee please destroy the message and all copies. Any 
opinion or information contained in this email or its attachments that does not 
relate to the business of Kainos 
is personal to the sender and is not given by or endorsed by Kainos. Kainos is 
the trading name of Kainos Software Limited, registered in Northern Ireland 
under company number: NI19370, having its registered offices at: Kainos House, 
4-6 Upper Crescent, Belfast, BT7 1NT, 
Northern Ireland. Registered in the UK for VAT under number: 454598802 and 
registered in Ireland for VAT under number: 9950340E. This email has been 
scanned for all known viruses by MessageLabs but is not guaranteed to be virus 
free; further terms and conditions may be 
found on our website - www.kainos.com 




[jQuery] Re: Is A Child Of?

2009-01-22 Thread Richard D. Worth
My first thought was to try these and neither worked

$(b, a).length

a.find(b).length

Also, no luck here

$(b[0], a[0]).length

a.find(b[0]).length

In the end this is the one I could get to work

$(b).parents().filter(function() { return this === a[0]; }).length

- Richard

On Thu, Jan 22, 2009 at 4:25 AM, James Hughes  wrote:

>
>
> Hi,
>
> This is probably a really easy question an I apologise if it appear stupid
> but I am clueless right now.  Given 2 jQuery objects (a,b) how can I tell if
> b is a child of a?
>
> James
>
> 
> This e-mail is intended solely for the addressee and is strictly
> confidential; if you are not the addressee please destroy the message and
> all copies. Any opinion or information contained in this email or its
> attachments that does not relate to the business of Kainos
> is personal to the sender and is not given by or endorsed by Kainos. Kainos
> is the trading name of Kainos Software Limited, registered in Northern
> Ireland under company number: NI19370, having its registered offices at:
> Kainos House, 4-6 Upper Crescent, Belfast, BT7 1NT,
> Northern Ireland. Registered in the UK for VAT under number: 454598802 and
> registered in Ireland for VAT under number: 9950340E. This email has been
> scanned for all known viruses by MessageLabs but is not guaranteed to be
> virus free; further terms and conditions may be
> found on our website - www.kainos.com
>
>
>