开发者

Count characters in contentEditable DIV

I'm using this Plugin to count chars on inputs and textareas: http://cssglobe.com/post/7161/jquery-plugin-simplest-twitterlike-dynamic-character-count-for-textareas

I would need to count characters also in a DIV with the setting "contentEditable" set to True.

Is this possible modifiying this Plugin?

I think I would need to change something in this line:

            var count = $(obj).val().length;

But i really don't know how the contentEditable works... Any idea?

Thanks!

Edit:

I'm doing this as brettz9 suggested:

var method = $.inArray(obj.nodeName.toLowerCase(), ['textarea', 'input']) !== -1 ? 'val' : 'text';
var count = $(obj)[method]().length;

I just have one little problem on doing this for other field I required t have a min/max lenght (I have one input and one contentEditable)

This is that conditional part:

                if (other_required){
                if ($(other_required).val().开发者_JS百科length > 0 && available >= 0){
                    $(submit_button).attr("disabled", "");
                } else {
                    $(submit_button).attr("disabled", "disabled");
                }

i don't know how to declare that [method] var and use it with "other_required"


val() is used to get input values such as textarea, textbox, etc. Try text() instead.

EDIT:

Try this:

function count(obj) {
  var method = $.inArray(obj.nodeName.toLowerCase(), 
    ['textarea', 'input']) !== -1 ? 'val' : 'text';
  return $(obj)[method]().length;
}

And your code would look like:

if (other_required){
if (count(other_required) > 0 && available >= 0){
    $(submit_button).attr("disabled", "");
} else {
    $(submit_button).attr("disabled", "disabled");
}


You can do it like this:

// We'll assume it is contenteditable (which uses text, or could also use html (innerHTML) if you wanted to count markup length) 
//  if it is not a textarea or input (which uses value)
var method = $.inArray(obj.nodeName.toLowerCase(), ['textarea', 'input']) !== -1 ? 'val' : 'text';
var count = $(obj)[method]().length;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜