Skip to content

Instantly share code, notes, and snippets.

@digiguru
Created February 22, 2012 11:29
Show Gist options
  • Save digiguru/1884329 to your computer and use it in GitHub Desktop.
Save digiguru/1884329 to your computer and use it in GitHub Desktop.
Generic Pluralization Function
/**
* jQuery.bvplural
* Generic Pluralization Function
*
* @Author: Adam Hall
* @Date: 2012-02-21
* @Copyright: © Brand View
* @Version: 1.0.0
* http://www.brandview.co.uk
*
* JS Dependencies: /js/jquery/jquery-1.7.1.js
*
* Usage: given the following....
* <span id="MyTag">
* <span class="plural">You can create <span class="value">2</span> more users</span>
* <span class="single">You can create one more user</span>
* <span class="none">Contact us to add more users</span>
* </span>
* Use this line to automatically display the correct message....
* $("#MyTag").DisplayPluralized(0) // Displays : Contact us to add more users
* $("#MyTag").DisplayPluralized(1) // Displays : You can create one more user
* $("#MyTag").DisplayPluralized(255) // Displays : You can create 255 more users
*/
(function ($) {
$.fn.DisplayPluralized = function (countOfItems) {
return this.each(function () {
var $this = $(this),
$none = $this.find(".none").hide(),
$single = $this.find(".single").hide(),
$plural = $this.find(".plural").hide();
$this.find(".value").text(countOfItems);
if (!countOfItems || countOfItems === 0) {
$none.show();
} else if (countOfItems === 1) {
$single.show();
} else {
$plural.show();
}
});
};
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment