I have looked in to the jquery documentation of function end(),
definition : End the most recent filtering operation in the current chain and return the set of matched elements to its previous state.
i have understood the functionality, but i could not able to make out, where it is more helpful.
ex:
<p>text</p>
<p class="middle">Middle <span>text</span></p>
<p>text</p>
<script type="text/JavaScript">
alert(jQuery('p').filter('.middle').length); //alerts 1
alert(jQuery('p').filter('.middle').end().length); //alerts 3
alert(jQuery('p').filter('.middle').find('span')
</script>
i have understood the second line displaying //alerts 3
, but it can also be written as
alert(jQuery('p').length); //alerts 3
Then why extra two methods .filter
and .end()
,
please give me an example, where the .end() will be useful.
Middle text
` then $('p').filter('.middle').find('.i1').css('color', 'red').end().find('.i2').css('color', 'green')` http://plnkr.co/edit/tNWZM5lbWHiqjqQkJ7Yj?p=preview – Arun P Johny Aug 26 '13 at 06:45