javascript - How to add onkeypress event listener to input tag - Stack Overflow

After I grab an array of input element tags, I try to loop through them adding the onkeypress event lis

After I grab an array of input element tags, I try to loop through them adding the onkeypress event listener to each one of them.

My Code:

window.onload = function()
{
    //  Add the event listeners to input tags
    //      Get the array of input tags
    var inputTags = document.getElementsByClassName('validateInput');
    console.log(inputTags);
    //      Loop through them, adding the onkeypress event listener to each one
    for (var i = 0; i < inputTags.lenght; i++)
    {
        var tag = inputTags[i];
        var functionToAdd = function(event, tag)
        {
            isNumberOrDot(event, tag);
        };
        tag.addEventListener('keypress', functionToAdd, false);
    }
};

Question:

Why isn't tag.addEventListener('keypress', functionToAdd, false); not adding the onkeypress event listener?

After I grab an array of input element tags, I try to loop through them adding the onkeypress event listener to each one of them.

My Code:

window.onload = function()
{
    //  Add the event listeners to input tags
    //      Get the array of input tags
    var inputTags = document.getElementsByClassName('validateInput');
    console.log(inputTags);
    //      Loop through them, adding the onkeypress event listener to each one
    for (var i = 0; i < inputTags.lenght; i++)
    {
        var tag = inputTags[i];
        var functionToAdd = function(event, tag)
        {
            isNumberOrDot(event, tag);
        };
        tag.addEventListener('keypress', functionToAdd, false);
    }
};

Question:

Why isn't tag.addEventListener('keypress', functionToAdd, false); not adding the onkeypress event listener?

Share Improve this question asked Jan 30, 2014 at 21:06 sargassargas 6,1808 gold badges54 silver badges70 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 3

You have run into 3 issues in your code. First of all don't create functions inside loops, second is closure issue ( you will always get only last i value ) , third is that you have typo in length property, corrected code should be

window.onload = function()
{
    //  Add the event listeners to input tags
    //      Get the array of input tags
    var inputTags = document.getElementsByClassName('validateInput');
    console.log(inputTags);
    //      Loop through them, adding the onkeypress event listener to each one
    var functionToAdd = function(event, tag)
    {
        isNumberOrDot(event, tag);
    };
    for (var i = 0; i < inputTags.length; i++)
    {
        (function( i ) {
            inputTags[ i ].addEventListener('keypress', function( e ) {
                functionToAdd( e, inputTags[i] )    
            }, false);
        })( i );
    }
};

.length is spelled wrong above:

for (var i = 0; i < inputTags.length; i++) {}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744895424a4599666.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信