Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ability to define options on a per element basis. #20

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ Advanced usage:
safeClass: 'safe',
overClass: 'over',
thousandSeparator: ',',
optionsForElement: function(element){ return {} },
onOverCount: function(count, countable, counter){},
onSafeCount: function(count, countable, counter){},
onMaxCount: function(count, countable, counter){}
Expand All @@ -38,6 +39,7 @@ Advanced usage:
* `safeClass` - The CSS class applied to the counter element when it is within the maxCount figure. Defaults to `safe`.
* `overClass` - The CSS class applied to the counter element when it exceeds the maxCount figure. Defaults to `over`.
* `thousandSeparator` - The separator for multiples of 1,000. Set to `false` to disable. Defaults to `,`.
* `optionsForElement` - Function called before each selected element is processed. Should return option overrides to be used during processing of that element.
* `onOverCount` - Callback function called when counter goes over `maxCount` figure.
* `onSafeCount` - Callback function called when counter goes below `maxCount` figure.
* `onMaxCount` - Callback function called when in `strictMax` mode and counter hits `maxCount` figure.
Expand Down
27 changes: 23 additions & 4 deletions demo.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
<title>jquery.simplyCountable.js demo</title>
<style type="text/css" media="screen">
* {font-family:Helvetica, Arial, sans-serif;}
body {width:800px;}
pre {float:right; width:300px; background:#efefef; padding:6px 12px; margin:0; font:13px 'Courier New', Courier, monospace; font-weight:bold; color:#3b3b3b;}
body {width:1000px;}
pre {float:right; width:500px; background:#efefef; padding:6px 12px; margin:0; font:13px 'Courier New', Courier, monospace; font-weight:bold; color:#3b3b3b;}
form {margin-bottom:30px;}
.safe , .over {padding:3px; color:white; font-weight:bold;}
.safe {background:green;}
Expand All @@ -25,21 +25,26 @@
maxCount: 10,
countDirection: 'up'
});
$('.demo4 textarea').simplyCountable({
optionsForElement: function(element){
return {counter: '#' + $(element).attr('id') + '_counter'};
}
});
});
</script>
</head>
<body>
<h1>jQuery Simply Countable plugin demo</h1>
<form>
<fieldset><legend>Demo 1</legend>
<fieldset><legend>Demo character countdown</legend>
<p>You have <span id="counter"></span> characters left.</p>
<pre>$('#demo1').simplyCountable();</pre>
<p><textarea id="demo1" cols="50" rows="4"></textarea></p>
<p><input type="submit" value="Submit"></p>
</fieldset>
</form>
<form>
<fieldset><legend>Demo 2</legend>
<fieldset><legend>Demo counting words</legend>
<p>You have used <span id="counter2"></span> words.</p>
<pre>$('#demo2').simplyCountable({
counter: '#counter2',
Expand All @@ -51,6 +56,20 @@ <h1>jQuery Simply Countable plugin demo</h1>
<p><input type="submit" value="Submit"></p>
</fieldset>
</form>
<form class="demo4">
<fieldset><legend>Demo fields with independent countdowns</legend>
<p>You have <span id="demo4a_counter"></span> characters left.</p>
<pre>$('.demo4 textarea').simplyCountable({
optionsForElement: function(element){
return {counter: '#' + $(element).attr('id') + '_counter'};
}
});</pre>
<p><textarea id="demo4a" cols="50" rows="4"></textarea></p>
<p>You have <span id="demo4b_counter"></span> characters left.</p>
<p><textarea id="demo4b" cols="50" rows="4"></textarea></p>
<p><input type="submit" value="Submit"></p>
</fieldset>
</form>
<p>Copyright &copy; 2009-2013 <a href="http://www.aaronrussell.co.uk">Aaron Russell</a>.
</body>
</html>
4 changes: 3 additions & 1 deletion jquery.simplyCountable.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

$.fn.simplyCountable = function(options){

options = $.extend({
globalOptions = $.extend({
counter: '#counter',
countType: 'characters',
maxCount: 140,
Expand All @@ -24,6 +24,7 @@
safeClass: 'safe',
overClass: 'over',
thousandSeparator: ',',
optionsForElement: function(element){ return {} },
onOverCount: function(){},
onSafeCount: function(){},
onMaxCount: function(){}
Expand All @@ -33,6 +34,7 @@

return $(this).each(function(){

var options = $.extend({}, globalOptions, globalOptions.optionsForElement(this));
var countable = $(this);
var counter = $(options.counter);
if (!counter.length) { return false; }
Expand Down