javaScript:passing argument to eventListener

Francesco

i'm try to make something and i made this piece of code,but when i press the botton it's happend for a sec and then disappear,am i donig passing the arguments wrong or something? here the code is:

{
        var fil1;
        var rtextDiv;
        for (var i = 0; i < dmsg.getElementsByClassName('refilter').length; i++) {
            var refilterInput = dmsg.getElementsByClassName('refilter')[i];
            refilterInput.addEventListener('keyup', firstfilter(rtextDiv, fil1,refilterInput));
        }

    };

    function firstfilter(e, rtextDiv, fil1, refilterInput) {
        rtextDiv = refilterInput.parentNode.parentNode.getElementsByClassName('rtext')[0];
        while (rtextDiv.firstChild) {
            rtextDiv.removeChild(rtextDiv.firstChild);
        }
        fil1 = filteredPropertiesTable(res, refilterInput.value);
        rtextDiv.appendChild(fil1);
    };

edited as the comment said:

{
        var fil1;
        var rtextDiv;
        for (var i = 0; i < dmsg.getElementsByClassName('refilter').length; i++) {
            var refilterInput = dmsg.getElementsByClassName('refilter')[i];
            refilterInput.addEventListener('keyup', function()
           {firstfilter(rtextDiv,fil1,refilterInput)(rtextDiv, fil1,refilterInput)});
         );
        }

    };

    function firstfilter(e, rtextDiv, fil1, refilterInput) {
        rtextDiv = refilterInput.parentNode.parentNode.getElementsByClassName('rtext')[0];
        while (rtextDiv.firstChild) {
            rtextDiv.removeChild(rtextDiv.firstChild);
        }
        fil1 = filteredPropertiesTable(res, refilterInput.value);
        rtextDiv.appendChild(fil1);
    };

is it true know?can i pass argument that way?

Leo

Here you are actually executing the handler:

refilterInput.addEventListener('keyup', firstfilter(rtextDiv, fil1,refilterInput));

You should just present the handler name:

refilterInput.addEventListener('keyup', firstfilter);

And the handler can be improved:

function firstfilter(e) {
    var rtextDiv = this.parentNode.parentNode.getElementsByClassName('rtext')[0];
    while (rtextDiv.firstChild) {
        rtextDiv.removeChild(rtextDiv.firstChild);
    }
    var fil1 = filteredPropertiesTable(res, this.value); // you didn't say what is res
    rtextDiv.appendChild(fil1);
};

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

javascript passing argument in object

Passing Argument to JavaScript Object Getter

Javascript passing element Id as argument

Javascript / Passing the right optional argument

Javascript passing argument: Strange Syntax

Passing an Object as an Argument in Javascript - But not simply an Argument but an argument built from variables

Adding an EventListener with an function argument

passing array as javascript function argument from php

Passing div style as a function argument Javascript/ React

Passing argument to Go IIFE (following javascript example)

Passing argument through an onchange method in Javascript

Javascript, passing my string as an argument turns it to a 0

Passing argument to the function inside the object in Javascript

Passing javascript object as an argument to a function - typescript equivalent

Passing an argument to a Javascript function through PHP

Passing variable into arrow function in eventListener

Javascript - change outside variable inside a function after passing it as an argument

Passing jQuery data table row as argument to JavaScript function

Transform argument before passing to a function without use of a closure in javascript

Passing a list of string as an argument to javascript function from native java code

passing single argument into a function that requires multiple arguments in javascript

Adding eventlistener to javascript array

JavaScript closure within eventlistener

javascript terminate onkeydown eventlistener

Cannot remove eventlistener on Javascript

scroll eventListener not working in javascript

FOSRestBundle "avoid" Javascript eventListener

javascript eventlistener not working

JavaScript eventListener mousemove