javascript - How to check function to see if parent exists -




working on js function: how check see if parent element exists , if doesn't print out 'not found'. how check parent element classname?

var findparentbyclassname = function(element, targetclass) { if (element) { var currentparent = element.parentelement; while (currentparent.classname !== targetclass && currentparent.classname !== null) { currentparent = currentparent.parentelement; } // closes loop return currentparent; } // closes if statement };

i thinking write this:

if(element.parentelement !== targetclass) { console.log('parent not found'); }

this can example of closest() , get() jquery functions. of course in dom has parent, check special, form class="myclass":

var element = $(yourelement).closest('form.myclass');  if(element.get(0)){  //you got element  }  else{   console.log('not found');  } 

the method get([index]) needed due, selector won't return empty.





wiki

Comments

Popular posts from this blog

Asterisk AGI Python Script to Dialplan does not work -

python - Read npy file directly from S3 StreamingBody -

kotlin - Out-projected type in generic interface prohibits the use of metod with generic parameter -