views:

40

answers:

3

I have writtern a script to limit the number of characters in the textarea, and the code is here

<script>
function limitchar(charcount, counterId, msgId)
{
    var tex = document.getElementById(msgId).value;
    var len = tex.length;
        if(len > charcount)
        {
            alert("Content Limit Exceeded");     
            tex = tex.substring(0,charcount);
            document.getElementById(msgId).value =tex;
            return false;
        }
    document.getElementById(counterId).innerHTML = charcount-len;
}
</script>

I am calling the function as

<textarea name="txtar1" id="txtar1"  onkeyup=limitchar('10','verid','txtar1')></textarea>

I dont want these kind of ugly function call, since my textareas are dynamically generated

Like Jquery, I want my function to be called like

$('txtar1').limitchar();

Is there anyother way to achieve this. Thanks Experts! in advance

ANSWER :

Thanks for andy rose. I used his approach. Here is my final code:

<script>
    $(document).ready(function() {
        $('#txtar1').keydown(function() {
            limiter('10', 'verid' , this.id);
        });
    });
</script>


<textarea name="txtar1" id="txtar1"></textarea>
    <span id="verid">10</span>

<script>
    /* Will use as common.js */
    function limiter(charcount,showcountid,msgId)
    {
        var tex = document.getElementById(msgId).value;
            var len = tex.length;
            if(len > charcount)
            {
           //alert("Content Limit Exceeded");      
           tex = tex.substring(0,charcount);
           document.getElementById(msgId).value =tex;
           return false;
             }
             document.getElementById(showcountid).innerHTML = charcount-len;
    }
</script>
+4  A: 

You might want to look at this blog entry from Mike Alsup in which he explains a pretty good pattern on how to develop a jQuery-Plugin. He also goes into very much details about further development and extending the basic plugin.

Bobby
Awesome, I've used that pattern years ago, and I still recommend it to every newcomer asking me for a good place to start. :) My own pattern has very slightly changed since then, but it's still a great first step. One point to note: The metadata plugin is called differently now. In 'Support the metadata plugin' (which I recommend highly), line 9 should now read: var o = $.metadata ? $.extend({}, opts, $this.metadata()) : opts;
Thomas
@Thomas: You might wanna post a comment on the entry (if it hasn't been pointed out yet) so that he can fix it.
Bobby
IIRC it has been pointed out there a couple of years back. :) Besides, it's a good exercise for the learner. :p
Thomas
Even the link is gud, @Andy Rose, approach works enough for me. Thanks for the great link!
Rajasekar
A: 

It's easy. Just do:

$.fn.limitchar = function () {
    var $items = $(this);
};

$items will be a jQuery object equivalent to $('txtar1').

Skilldrick
+2  A: 

Rather than creating a new plugin why not use one of jQuery's key events:

$('txtar1').keydown(function() {
  ...stuff here
});
Andy Rose