Created
December 19, 2014 00:22
-
-
Save brandonjp/5ad07e713c4302fbd0b3 to your computer and use it in GitHub Desktop.
jQuery thisOrNothin() firstOrNothin() - get a real NULL if nothing matches, current stack is empty or elem.length is zero 0
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function($) { | |
$.fn.thisOrNothin = $.fn.thisOrNothing = function(search) { | |
// thisOrNothing(search) - gives back what you gave it, unless it's empty | |
// use the optional search to filter by selectors | |
// EX: $('div').thisOrNothing('.hidden') | |
// *Note: Don't you go chainin' this now... if it's NULL it'll blow | |
// First attempt to filter | |
var filtered = search ? this.filter(search) : this; | |
// Return the filtered collection if length, else return null | |
return (filtered.length) ? filtered : null; | |
}; | |
})(jQuery); | |
(function($) { | |
$.fn.firstOrNothin = $.fn.firstOrNothing = function(search) { | |
// firstOrNothing(search) - returns a collection's first matched element, unless it's empty | |
// use the optional search to filter by selectors | |
// EX: $('div').firstOrNothing('hidden') | |
// *Note: This ain't be for chainin' things... so best just don't | |
// First attempt to filter | |
var filtered = search ? this.filter(search) : this; | |
// Return the filtered first element if any, else return null | |
return (filtered.length) ? filtered.eq(0) : null; | |
}; | |
})(jQuery); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment