Home  >  jQuery

Example of .andSelf() in jQuery

February 20, 2013
.andSelf() is called on any defined element of html. If there are elements in the stack for any purpose, then .andSelf() add the self element in the current stack as
$('li.test').nextAll().andSelf().css('background-color', 'red');
 
Once all the element are in stack then we can process any css property like background-color etc. Below is the complete example where we process li element.
<!DOCTYPE html>
<html>
<head>
<script src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>
<ul>
<li>A</li>
<li class="test">B</li>
<li>C</li>
<li >D</li>
</ul>
<script>

$('li.test').nextAll().andSelf()
.css('background-color', 'green');

</script>
</body>
</html>
 
Output will look like
  • A
  • B
  • C
  • D
FIND MORE TUTORILAS






©2019 concretepage.com | Privacy Policy | Contact Us