最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - onkeyup or onkeypress only after X characters length - Stack Overflow

programmeradmin5浏览0评论

There is anyway to call a js function only when we fill a number of characters characters on a textfield?

from now im using onblur="myFunction();" but i wants to cal that function after we have some characters filled.

Thank you!

There is anyway to call a js function only when we fill a number of characters characters on a textfield?

from now im using onblur="myFunction();" but i wants to cal that function after we have some characters filled.

Thank you!

Share Improve this question edited Jul 20, 2013 at 0:54 dana 18.2k7 gold badges68 silver badges90 bronze badges asked Jul 20, 2013 at 0:41 FbInfoFbInfo 1032 silver badges7 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 6

Use the onkeyup event:

let element = document.querySelector('your element');
element.onkeyup = function () {
    if (this.value.length > 10) {
        myFunction();
    }
}

You also use this in an attribute:

<textarea onkeyup="if (this.value.length > 10) myFunction();"></textarea>

EXAMPLE (for both)

/* go automatic do the next tabindex */ 

$(":input:not(:disabled)").not($(":button")).keyup(function(evt){
if (this.value.length == 1) {
    currentTabindex = $(this).attr('tabindex');
            if (currentTabindex) {
                nextInput = $('input[tabindex^="'+ (parseInt(currentTabindex)+1) +'"]');
                if (nextInput.length) {
                    nextInput.focus();
                    return false;
                }
            }
}
});
发布评论

评论列表(0)

  1. 暂无评论