Put the code inside the
$("[EMAIL PROTECTED]").each(function() {
...
})
into
jQuery.fn.countSize(function() { // create a jquery method called
countSize
return this.each(function() { // for each item in the jquery array,
run this function
...
});
});
A developer will then be able to do
$("[EMAIL PROTECTED]").countSize();
The idea is that a developer could apply your plugin to all textareas on a
page, or just a particular one, by changing "[EMAIL PROTECTED]".
I used countSize in this example but you can use any function name you like.
Blair
On 1/20/07, Bjorn Wijers <[EMAIL PROTECTED]> wrote:
Hi,
My name is Bjorn Wijers and I recently started with jQuery. I have tried
other libs as well, but this seems to be the one best suited for me.
Thanks to John and all the other contributors for making jQuery available!
I started out with a small project which is based on this example [1] by
Peter-Paul Koch of Quirksmode [2] fame. In his example he created a text
counter for textarea's so one can inform a user when he/she has typed
the maximum amount of characters in a textarea. The maximum amount of
chars is set by the maxlength attribute of the textarea and is also used
in finding all textareas and add this feature. I tried to rewrite his
example in jQeury while adding a new feature which prevents a user to
type more than the amount specified.
My question is:
How can I create a jQuery plugin from this code so that other people can
use this as well. I want to make it as easily useable as possible :)
CODE:
-----------
$(document).ready(function(){
// set the max chars
$("[EMAIL PROTECTED]").each(function(){
var max = this.getAttribute('maxlength');
var html_counter = "<div class=\"counter\"><span>0</span>/" + max
+"</div>";
$(this).before(html_counter);
this.relatedElement = $('span')[0];
});
// check the max chars
$("[EMAIL PROTECTED]").keyup(function(){
var maxLength = this.getAttribute('maxlength');
var currentLength = this.value.length;
if(currentLength >= maxLength) {
this.relatedElement.className = 'toomuch';
this.value = this.value.substring(0, maxLength);
} else {
this.relatedElement.className = '';
}
this.relatedElement.firstChild.nodeValue = currentLength;
});
});
LINKS:
----------
[1] http://www.quirksmode.org/book/examplescripts/maxlength/index.html
[2] http://www.quirksmode.org/
All the best,
grtz
BjornW
* b u r o b j o r n .nl *
digitaal vakmanschap | digital craftsmanship
_______________________________________________
jQuery mailing list
[email protected]
http://jquery.com/discuss/
_______________________________________________
jQuery mailing list
[email protected]
http://jquery.com/discuss/