Last active
September 4, 2015 14:10
-
-
Save jakerb/12d3267b3e98307d75d1 to your computer and use it in GitHub Desktop.
Automatically adds ordinals to numbers (1,2,3 -> 1st, 2nd, 3rd)
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 ordinaljs(e,t){var n=0;if(!e){var n=1;console.log("Ordinal: What should ordinal apply to?")}if(!t){var n=1;console.log("Ordinal: Set Superscript to either on or off.")}if(n!=1){$(e).each(function(){var e=$(this).text();if(t){$(this).html(e+"<sup>"+ordinal_switch(e)+"</sup>")}else{$(this).text(e+ordinal_switch(e))}})}}function ordinal_switch(e){var t=parseInt(e.slice(-1));if($.isNumeric(t)){switch(t){case 0:return"th";break;case 1:return"st";break;case 2:return"nd";break;case 3:return"rd";break;default:return"th"}}} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Ordinal.js
Automatically adds ordinals to numbers (22 becomed 22nd, 33 becomes 33rd) within a HTML document using jQuery.
To use this simply, add a selector (<span> in this case) or specific ID or class to the number you wish ordinal.js to modify and instantiate the function.
To instantiate, simply add:
<script>ordinaljs("selector[id/class]","superscript[true/false]");</script>
Here is what a demo page looks like or check it out on Codepen: