Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery: if (target) is child of ('.wrapper') then (do something)

var target = $(this).attr("href");  if {target is child of ('.wrapper')} then (do something) 

simple syntax? can someone show me the correct syntax here?

like image 366
android.nick Avatar asked Oct 18 '10 06:10

android.nick


2 Answers

if($(target).parents('.wrapper').length > 0) {    //do something... } 
like image 70
Jacob Relkin Avatar answered Oct 10 '22 02:10

Jacob Relkin


.has() is maybe the mose convenient syntax:

if( $('.wrapper').has($(target)) ) {      // do something } 

Even more 'powerful' (in terms of performance) is $.contains(). So an ideal algorithm should look like:

var $wrapper =  $('.wrapper'),     $target  =  $(this).attr('href');  if( $.contains($wrapper[0], $target[0]) ) {     // do something } 

Reference: .has(), $.contains()

like image 45
jAndy Avatar answered Oct 10 '22 00:10

jAndy