Jquery Ancestor

Jquery parent()

The parent() method returns the direct parent element of selected element.

$(document).ready(function(){
 $('span').parent().addClass('active');
});


Jquery parents()

The parents() method returns all parent element of selected element.

$(document).ready(function(){
 $('span').parents().css({
  fontSize: '20px',
  color: 'black'
 });
});


Jquery parents() - filter

The parents() - filter method use an optional parameter to filter the search for ancestor.

$(document).ready(function(){
 $('span').parents('ul').css({
  fontSize: '20px',
  color: 'black'
 });
});


Jquery parentsUntil()

The parentsUntil() method returns all ancestors elements between two given arguments.

$(document).ready(function(){
 $('span').parentsUntil('div').css({
  fontSize: '20px',
  color: 'black'
 });
});

Share this

Related Posts