Setting maxlength using javascript

I am trying to set maxlength on input fields dynamically using JavaScript. This seems to be a problem in IE, and I found part of the solution.

$("input#title").get(0).setAttribute("max_length", 25);
$("input#title").get(0).setAttribute(
                        "onkeypress", 
                        "return limitMe(event, this)");

function limitMe(evt, txt) {
    if (evt.which && evt.which == 8) return true;
    else return (txt.value.length < txt.getAttribute("max_length");
}

It works in Firefox, but not in IE for some reason. However, it works with input fields defined as follows:

<input type="text" max_length="25" onkeypress="return limitMe(event, this);"/>

But since input fields are created dynamically, I can’t do this ... Any ideas?

+5
source share
7 answers

If you use jQuery, why not use its abstractions to the fullest?

eg.

Instead:

$("input#title").get(0).setAttribute("max_length", 25);
$("input#title").get(0).setAttribute(
                        "onkeypress", 
                        "return limitMe(event, this)");
function limitMe(evt, txt) {
    if (evt.which && evt.which == 8) return true;
    else return (txt.value.length < txt.getAttribute("max_length");
}

Do it:

$('input#title').attr('maxLength','25').keypress(limitMe);

function limitMe(e) {
    if (e.keyCode == 8) { return true; }
    return this.value.length < $(this).attr("maxLength");
}

: , IE, , , onKeyPress setAttribute. - .

+8

, , maxlength, max_length. . .

+5

, maxlength JavaScript, - maxlength , . .

+2

IE setAttribute(), "maxLength"...

var el=document.createElement('input'); el.setAttribute('maxLength',25);
+2

/. , input:

$('input').on('input', onInput);

var inputValue = '';

function onInput(e) {
    if(e.currentTarget.value.length > 30) {
        e.currentTarget.value = titleValue;
        return;
    }

    inputValue = e.currentTarget.value;
}
+1
source

Since I had some problems with the answer from @James, I wrote sth, which worked even when copying the insert, and especially when working with IE8 and below. My implementation uses jQuery and its live events.

jQuery(function () {
    $('body').on('keydown.maxlength_fix', 'textarea[maxlength]', function (e) {
        var $this = $(this);
        window.setTimeout(function () {
            var val = $this.val();
            var maxlength = $this.attr('maxlength');
            if (val.length > maxlength) {
                $this.val(val.substr(0, maxlength));
            }
        }, 4);
    });
});
0
source

My code also prints the written / max characters, ignoring this part.

$("[maxlength]").bind("keyup focusin", function(){
    var maxkey = $(this).attr("maxlength");
    var length = $(this).val().length;
    var value = $(this).val();
    $(this).parent().find(".counter").text(length+"/"+maxkey);
    if (length > maxkey) $(this).val(value.substring(0, maxkey));
}).bind("focusout", function(){$(this).parent().find(".counter").text("")});
0
source

All Articles