Extend your jQuery selector

Source: http://www.websanova.com/tutorials/jquery/12-awesome-jquery-selector-extensions

Example:

### Select everything that has ID on the tag ###
$.extend($.expr[':'],
{
    hasId: function(el)
    {
        return $(el).attr('id') !== undefined && $(el).attr('id') !== '';
    }
}
$("#container :hasId");

### Select everything that's current in "view" ###
$.extend($.expr[':'],
{
    inView: function(el)
    {
        var offset = $(el).offset();

        return !(
            (offset.top > $(window).height() + $(document).scrollTop()) || 
            (offset.top + $(el).height() < $(document).scrollTop()) || 
            (offset.left > $(window).width() + $(document).scrollLeft()) || 
            (offset.left + $(el).width() < $(document).scrollLeft())
        )
    }
}
$("#container :inView");

Leave a Reply