0

I'm using jQuery plugin called charCount to display a character counter for some textareas, it works great, however when I add new text areas on the page (dinamically), the new text areas doesn't have the counter, I'm newbie, here is how I use the plugin:

$('.message-form').charCount({ allowed: 140, warning: 20 });

Update Solution:

$(".message-form").live("click", function(){
        if ($(this).data('hascharcount') == undefined) {
            $(this).data('hascharcount', true).charCount({ allowed: 140, warning: 20 });
        }
    });
Special K.
  • 521
  • 1
  • 8
  • 18
  • 1
    `live` is for events. Call the method right after appending new elements. – Ram Dec 29 '12 at 20:11
  • @undefined `live` is deprecated. Use `on` for events instead. – John Dvorak Dec 29 '12 at 20:14
  • @JanDvorak Yes, I [know](http://stackoverflow.com/questions/14081210/jquery-only-partially-working-on-click-function/14081242#14081242). – Ram Dec 29 '12 at 20:16
  • You're not binding anything there, you can't use that line as a live event. Show us the method charCount(). – Popnoodles Dec 29 '12 at 20:17
  • @popnoodles http://cssglobe.com/lab/charcount/charCount.js, thank you. – Special K. Dec 29 '12 at 20:22
  • so it's a plugin. Rather than fiddle with a plugin because the author might publish an update, I'd write around it, which is what I've done anyway in my answer. – Popnoodles Dec 29 '12 at 20:23

4 Answers4

0

It sounds like you need to initialize the charCount on each text area after you dynamically add them to the page. If you only have the line above, it will only impact the textareas that are on the page at the time the page loads. So you would do something like this:

//add the textarea with an ID of 'newTextarea1'
//then use charCount on the new textarea
$('#newTextarea1').charCount({ allowed: 140, warning: 20 });
davehale23
  • 4,374
  • 2
  • 27
  • 40
  • I need to use the live because I have around 200 text inputs, or even more, initializing each time a new text area is added isn't an option for me. Thank you. – Special K. Dec 29 '12 at 20:19
0

The simplest way would just be to run that method on any new elements created.

$('<textarea ...>').appendTo('#wherever').charCount({ allowed: 140, warning: 20 });

But you asked about doing it using on() which means you don't want to do that.

But

$('.message-form').charCount({ allowed: 140, warning: 20 });

can't simply be made to use on() as on() binds an event. Rather than edit a plugin which may have updates by the author at any time which would mean re-fiddling it, write around it.

So, if you don't want to call .charCount each time after dynamically creating the elements, you can do this which will call it contextually (i.e. not until the user actually uses the element).

$(document).on('focus', '.message-form', function(){
    if (typeof $(this).data('hascharcount')=='undefined') {
        $(this).data('hascharcount', true).charCount({ allowed: 140, warning: 20 });
    }
})

N.B. A lot of people assume .on() is an alias of the depreciated .live() which it isn't. To use on() as live(), i.e. for elements that don't exist at the time it's run, it needs something that already exists to anchor to such as the parent div that content is created inside of or, if you're lazy, document.

Popnoodles
  • 28,090
  • 2
  • 45
  • 53
  • Thank you, I've finished my code based on your ideea by setting a value to the element and check if it's defined or not. – Special K. Dec 29 '12 at 21:38
0

Try using .on() with the focusin event while targeting the parent container of your selector .message-form like :

$("#parentContainer").on("focusin", function(){
  $('.message-form').charCount({ allowed: 140, warning: 20 });
}); // on

... or you could target the body tag or the document instead like :

$("body").on("focusin", function(){
  $('.message-form').charCount({ allowed: 140, warning: 20 });
}); // on

... this will allow you to use your plugin with present and future elements (no need to re-initialize your plugin every time you add a new element)

I have successfully used that method with other plugins that don't support dynamically added elements as you could see here https://stackoverflow.com/a/9084293/1055987

NOTE : .on() requires jQuery v1.7+, otherwise .delegate() is recommended for lower versions.

EDIT : I was expecting to find a callback within the Character Count plugin but it doesn't exist ...so, in order to avoid the counter duplication, we could tweak the code this way :

$("#myForm").on("focusin", '.message-form', function() {
    $(this).next("span.counter").remove();
    $(this).charCount({
        allowed: 140,
        warning: 20
    });
}); // on

See working DEMO

Community
  • 1
  • 1
JFK
  • 40,963
  • 31
  • 133
  • 306
  • Hey, thanks - it does work, but if I go into another textarea it duplicates the values to all other text areas, if I go first into textarea1, it will show 140 chars left, however, if I enter the mouse on second one, the textarea1 will get 140140, any way of removing that? – Special K. Dec 29 '12 at 21:06
  • @SpecialK. I don't know the plugin but I will check it out – JFK Dec 29 '12 at 21:08
  • thank you, I guess that hapens because of the 'focusin' event, when the mouse focus in a textarea it will show the chars left multiple times, if I go 3 times it will show me 140140140. – Special K. Dec 29 '12 at 21:15
-4

When you add a new textarea, look within the element you add it using find() for the new one and call plugin on that element only

Assuming you have some sort of row structure, or wrapper for the textarea:

var newDiv=$('<div class="foo"><textarea class="message-form"></textarea></div>').appendTo('#content')

newDiv.find('.message-form').charCount({ allowed: 140, warning: 20 });
charlietfl
  • 170,828
  • 13
  • 121
  • 150
  • Response made 10 minutes before you posted this to the same answer as this that someone else already posted "I need to use the live because I have around 200 text inputs, or even more, initializing each time a new text area is added isn't an option for me" – Popnoodles Dec 29 '12 at 20:32
  • 2
    @popnoodles I have no idea what you are talking about – charlietfl Dec 29 '12 at 20:35