jQuery conditional chaining

Create jQuery chaining with customized conditional control by filter
and end that are built-in methods of jQuery.

This trick can simplify you JavaScript codes to a single chain and can be maintained
more easliy.

All conditions and each corresponding action are put together into a singal
line. If you wanna disable one of contition controls. Just comment that line.

1
2
3
4
5
$('.your_selector')  
// equivalent to: if (foo === bar) { $(this).css('display', 'none')}
.filter(function () { return foo == bar; }).css('display', 'none').end()
// equivalent to: if ($(this).hasClass('foo'){ $(this).html('bar'); }
.filter(function () { return $(this).hasClass('foo'); }).html('bar').end();