1

I have a div element .userbio , which is used to display "About me". When you click on the element, it becomes a textarea, where i can edit the information. But it should stop, when the character limit reaches 180. A plugin is being used for editing the "about me" section https://code.google.com/p/jquery-in-place-editor/

On clicking the .userbio element, .editInPlace class of the plugin gets attached to the .userbio element. So i've tried setting css .editInPlace textarea { maxlength:180 }, it doesn't work.

What happens now is that, i can type in as many characters i can and it overflows the entire page. I want the editing to stop, when it reaches the limit of 180 characters. Even tried some Jquery code snippets, but it doesn't work. Thank You

sandesh ps
  • 43
  • 1
  • 9

4 Answers4

1

You could bind a keyup event and substring the text if it exceeds the character count limit...

var limit = 180;

$("textarea").on("keyup", function() {
  var val = $("textarea").val();
  if(val.length > limit)
    $("textarea").val(val.substr(0, limit));
});
textarea {
  width: 100%;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<textarea>text</textarea>
LcSalazar
  • 16,524
  • 3
  • 37
  • 69
  • This code works fine, but there is one problem. My div element in console upon clicking on it, looks like this `class="userbio editInPlace-active"`. So, i tweaked your code a bit and wrote `$('.userbio .editInPlace-active')`, but it doesn't work – sandesh ps Sep 23 '14 at 15:54
  • You can use `$(".userbio").on("keyup", "textarea", function() {` – LcSalazar Sep 23 '14 at 15:58
1

I would consider adding the code for maxlength to the jquery plugin. From what I can see this is controlled in the function createEditorElement so just add that

createEditorElement: function() {
    if (-1 === $.inArray(this.settings.field_type, ['text', 'textarea', 'select']))
            throw "Unknown field_type <fnord>, supported are 'text', 'textarea' and 'select'";

    var editor = null;
    if ("select" === this.settings.field_type)
            editor = this.createSelectEditor();
    else if ("text" === this.settings.field_type)
            editor = $('<input type="text" ' + this.inputNameAndClass() 
                    + ' size="' + this.settings.text_size  + '" maxlength="' + this.settings.text_max + '" />');
    else if ("textarea" === this.settings.field_type)
            editor = $('<textarea ' + this.inputNameAndClass() 
                    + ' rows="' + this.settings.textarea_rows + '" '
                    + ' cols="' + this.settings.textarea_cols + '" maxlength="' + this.settings.textarea_max + '"/>');

    return editor;
}

Then add the default values for text_max and textarea_max to the top default settings object

$.fn.editInPlace.defaults = {
    text_max: 180,
    textarea_max: 180,
    .... rest of defaults ....
}
Artjom B.
  • 61,146
  • 24
  • 125
  • 222
pln
  • 1,108
  • 9
  • 13
1

We have to find a way to add :

$(".editInPlace").attr("maxLength", 180);

Somewhere.

So we have to find a "hook" to set this attribute to the textarea when it pops out. I tried to look for this kind of hook in the documentation of the plugin but did not find one.

If nobody finds a better way to solve your problem, you can change the library's source code directly.

createEditorElement: function() {
    if (-1 === $.inArray(this.settings.field_type, ['text', 'textarea', 'select']))
        throw "Unknown field_type <fnord>, supported are 'text', 'textarea' and 'select'";

    var editor = null;
    if ("select" === this.settings.field_type)
        editor = this.createSelectEditor();
    else if ("text" === this.settings.field_type)
        editor = $('<input type="text" ' + this.inputNameAndClass() 
            + ' size="' + this.settings.text_size  + '" />');
    else if ("textarea" === this.settings.field_type)
        editor = $('<textarea ' + this.inputNameAndClass() 
            + ' rows="' + this.settings.textarea_rows + '" '
            + ' cols="' + this.settings.textarea_cols + '" />');

    return editor;
},

And before returning "editor", add something like this:

if(this.settings.textarea_maxLength) {
    editor.attr("maxLength", this.settings.textarea_maxLength);
}

Then you'll have to set the "maxLength" attribute as an option field when you instantiate the editInPlace:

$("#editme1").editInPlace({
    /* the options you had before */
    textarea_maxLength: 180
});

But it seems a bit tricky and I didn't test it, maybe you should consider asking the plugin's developer directly.

oops I didn't refresh the page before posting, sorry. This is basically the same solution as @pln

Artjom B.
  • 61,146
  • 24
  • 125
  • 222
Javier92
  • 773
  • 6
  • 12
-2

In jQuery, you can achieve the effect by disabling the input when the length of the inner text exceeds 179 characters:

if ( $('.userbio').text().length > 179 ) {
    $('.userbio').prop('disabled', true);
}

Alternatively, HTML5 supports a maxlength property on textarea elements: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/textarea

rnevius
  • 26,578
  • 10
  • 58
  • 86