<html>
<head>
<script class="jsbin" src="http://ajax.googleapis.com/ajax/libs/mootools/1.2.4/mootools-yui-compressed.js"></script>
<meta charset=utf-8 />
<title>JS Bin</title>
<!--[if IE]>
<script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<style>
article, aside, figure, footer, header, hgroup,
menu, nav, section { display: block; }
</style>
</head>
<body>
<h1 id="total">$0.99</h1>
<button id="high">Change to $999.99</button>
<button id="med">Change to $499.02</button>
<button id="low">Change to $0.00</button>
<script type="text/javascript" charset="utf-8">
//<![CDATA[
window.addEvent('domready', function(){
var total = $('total');
var totalFx = new Fx.CashRegister(total, {duration: 2000});
$('high').addEvent('click', function(){
totalFx.start(999.99);
});
$('med').addEvent('click', function(){
totalFx.start(499.02);
});
$('low').addEvent('click', function(){
totalFx.start(0.02);
});
});
//]]>
</script>
</body>
</html>
Fx.CashRegister = new Class({
Extends: Fx,
options: {
precision: 2,
prefix: '$',
thousandsSeparator: ',',
decimalSeparator: '.'
},
initialize: function(element, options){
this.element = this.subject = document.id(element);
this.parent(options);
},
start: function(to){
if (!this.check(to)) return this;
var current_value = this.element.get('text');
current_value = current_value.substring(this.options.prefix.length);
current_value = this.stripSeparators(current_value);
current_value = current_value.toFloat().round(this.options.precision);
this.from = current_value;
this.to = to;
this.time = 0;
this.transition = this.getTransition();
this.startTimer();
this.onStart();
return this;
},
set: function(now){
var new_value = this.options.prefix + now.round(this.options.precision);
this.element.set('text', this.addSeparators(new_value));
},
addSeparators: function(number)
{
var parts = number.toString().split(this.options.decimalSeparator);
var dollars = parts[0];
if (parts[1]) { var cents = parts[1]; }
var rgx = /(\d+)(\d{3})/;
while (rgx.test(dollars)) {
dollars = dollars.replace(rgx, '$1' + this.options.thousandsSeparator + '$2');
}
var output = dollars;
if (this.options.precision > 0) {
if (cents) {
output += this.options.decimalSeparator + cents;
} else {
output += '0' * this.options.precision;
}
}
return output;
},
stripSeparators: function(string) {
var result = string.replace(this.options.decimalSeparator, '.');
var pattern = new RegExp(this.options.thousandsSeparator, 'g');
var matches = result.match(pattern)
if (matches) {
matches.each(function() {
result = string.replace(pattern, '');
});
}
return result;
}
});
Output
This bin was created anonymously and its free preview time has expired (learn why). — Get a free unrestricted account
Dismiss xKeyboard Shortcuts
Shortcut | Action |
---|---|
ctrl + [num] | Toggle nth panel |
ctrl + 0 | Close focused panel |
ctrl + enter | Re-render output. If console visible: run JS in console |
Ctrl + l | Clear the console |
ctrl + / | Toggle comment on selected lines |
ctrl + ] | Indents selected lines |
ctrl + [ | Unindents selected lines |
tab | Code complete & Emmet expand |
ctrl + shift + L | Beautify code in active panel |
ctrl + s | Save & lock current Bin from further changes |
ctrl + shift + s | Open the share options |
ctrl + y | Archive Bin |
Complete list of JS Bin shortcuts |
JS Bin URLs
URL | Action |
---|---|
/ | Show the full rendered output. This content will update in real time as it's updated from the /edit url. |
/edit | Edit the current bin |
/watch | Follow a Code Casting session |
/embed | Create an embeddable version of the bin |
/latest | Load the very latest bin (/latest goes in place of the revision) |
/[username]/last | View the last edited bin for this user |
/[username]/last/edit | Edit the last edited bin for this user |
/[username]/last/watch | Follow the Code Casting session for the latest bin for this user |
/quiet | Remove analytics and edit button from rendered output |
.js | Load only the JavaScript for a bin |
.css | Load only the CSS for a bin |
Except for username prefixed urls, the url may start with http://jsbin.com/abc and the url fragments can be added to the url to view it differently. |