Can there be or is there a Javascript addEventListener multiple arguments on single line helper function? - Stack Overflow

Can there be or is there something already, that can allow me to add multiple event listeners with mult

Can there be or is there something already, that can allow me to add multiple event listeners with multiple functions for different elements... on a single line? Maybe an example will help me explain...

I am ing across situations like this in my code:

inputUrl.addEventListener ('keydown', saveOptions);
inputUrl.addEventListener ('keydown', resizeInput);

inputUrl.addEventListener ('keyup', saveOptions);
inputUrl.addEventListener ('keyup', resizeInput);

inputUrl.addEventListener ('click', saveOptions);
inputUrl.addEventListener ('click', resizeInput);

inputDirectory.addEventListener ('keydown', saveOptions);
inputDirectory.addEventListener ('keydown', resizeInput);

inputDirectory.addEventListener ('keyup', saveOptions);
inputDirectory.addEventListener ('keyup', resizeInput);

inputDirectory.addEventListener ('click', saveOptions);
inputDirectory.addEventListener ('click', resizeInput);

It seems it might be VERY nice to have something along the way of this to shorten the code!

makeEvents("inputUrl,inputDirectory|saveOptions,resizeInput|keydown,keyup,click")

I hope this makes sense. Any ideas? If something like this already exists I would like to know. Otherwise maybe I could make a helper function (called makeEvents, or something), that would split the string, and for each element, then each function, attach each event?

I know my existing code logic doesn't make perfect sense at the moment, but I am mostly curious if this could be easily done or already exists.

Can there be or is there something already, that can allow me to add multiple event listeners with multiple functions for different elements... on a single line? Maybe an example will help me explain...

I am ing across situations like this in my code:

inputUrl.addEventListener ('keydown', saveOptions);
inputUrl.addEventListener ('keydown', resizeInput);

inputUrl.addEventListener ('keyup', saveOptions);
inputUrl.addEventListener ('keyup', resizeInput);

inputUrl.addEventListener ('click', saveOptions);
inputUrl.addEventListener ('click', resizeInput);

inputDirectory.addEventListener ('keydown', saveOptions);
inputDirectory.addEventListener ('keydown', resizeInput);

inputDirectory.addEventListener ('keyup', saveOptions);
inputDirectory.addEventListener ('keyup', resizeInput);

inputDirectory.addEventListener ('click', saveOptions);
inputDirectory.addEventListener ('click', resizeInput);

It seems it might be VERY nice to have something along the way of this to shorten the code!

makeEvents("inputUrl,inputDirectory|saveOptions,resizeInput|keydown,keyup,click")

I hope this makes sense. Any ideas? If something like this already exists I would like to know. Otherwise maybe I could make a helper function (called makeEvents, or something), that would split the string, and for each element, then each function, attach each event?

I know my existing code logic doesn't make perfect sense at the moment, but I am mostly curious if this could be easily done or already exists.

Share Improve this question edited Sep 18, 2019 at 17:38 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked Sep 24, 2012 at 19:13 theMaxxtheMaxx 4,1262 gold badges28 silver badges34 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 3

Try the following:

inputUrl.addEventListener ('keyup', function() { saveOptions(); resizeInput(); });

This way the 'keyup' event will call both functions and you have more control over the order that they are called in.

jquery can bind multiple events to multiple elements

$(inputUrl).add(inputDirectory).bind("keydown, keyup, click", function(){
     resizeInput(this);     
     saveOptions(this);
});

Lucky for you that someone invented functions

function setupEvents(element, action)
{
    element.addEventListener("keydown", action);
    element.addEventListener("keyup", action);
    element.addEventListener("click", action);
}

function setupActions(element)
{
    setupEvents(element, saveOptions);
    setupEvents(element, resizeInput);
}

setupActions(inputUrl);
setupActions(inputDirectory);

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

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

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

关注微信