Skip to content

Instantly share code, notes, and snippets.

@remi
Created October 4, 2012 14:51
Show Gist options
  • Save remi/3834040 to your computer and use it in GitHub Desktop.
Save remi/3834040 to your computer and use it in GitHub Desktop.
Got tired of using `if ($("#foo").length)`, so I wrote these helpers.
// Helpers
$.fn.isPresent = function() { return this && this.length > 0 }
$.fn.isBlank = function() { return typeof(this) == "undefined" || !this || (this && this.length == 0) }
// Examples
$("body").isPresent() // => true
$("#foo").isPresent() // => false
$("body").isBlank() // => false
$("#foo").isBlank() // => true
@rafbm
Copy link

rafbm commented Oct 4, 2012

isBlank could be as simple as:

$.fn.isBlank = function() { return !this || !this.length }

Loose typing FTW!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment