How to pass arguments to addEventListener listener function

0 votes

The situation is somewhat like-

var someVar = some_other_function();
someObj.addEventListener("click", function(){
    some_function(someVar);
}, false);

The problem is that the value of someVar is not visible inside the listener function of the addEventListener, where it is probably being treated as a new variable.

Sep 2, 2020 in Java-Script by kartik
• 37,510 points
7,897 views

1 answer to this question.

0 votes

Hello @kartik,

There is absolutely nothing wrong with the code you've written. Both some_function and someVar should be accessible, in case they were available in the context where anonymous

function() { some_function(someVar); } 

was created.

Check if the alert gives you the value you've been looking for, be sure it will be accessible in the scope of anonymous function (unless you have more code that operates on the same someVar variable next to the call to addEventListener)

var someVar; 
someVar = some_other_function();
alert(someVar);
someObj.addEventListener("click", function(){
    some_function(someVar);
}, false);

Hope it works!!

Thank You!!

answered Sep 2, 2020 by Niroj
• 82,880 points

Related Questions In Java-Script

0 votes
1 answer

How to pass an array as a function parameter in JavaScript?

Hello @kartik, Use: const args = ['p0', 'p1', 'p2']; call_me.apply(this, ...READ MORE

answered Sep 4, 2020 in Java-Script by Niroj
• 82,880 points
1,519 views
0 votes
1 answer

How to access PHP session variables from jQuery function in a .js file?

Hello, You can produce the javascript file via ...READ MORE

answered Apr 29, 2020 in Java-Script by Niroj
• 82,880 points
12,294 views
0 votes
1 answer

How to create dynamic href in react render function?

Hello @kartik, Use string concatenation: href={'/posts/' + post.id} The JSX ...READ MORE

answered May 18, 2020 in Java-Script by Niroj
• 82,880 points
10,609 views
0 votes
1 answer

How do I pass variables and data from PHP to JavaScript?

Hello @kartik, Simply use one of the following ...READ MORE

answered Jul 6, 2020 in Java-Script by Niroj
• 82,880 points
1,552 views
0 votes
1 answer

How to access PHP var from external javascript file?

Hello @kartik, You don't really access it, you ...READ MORE

answered Jul 6, 2020 in Java-Script by Niroj
• 82,880 points
7,167 views
0 votes
1 answer

How do I escape a single quote in SQL Server?

Hello @kartik, Single quotes are escaped by doubling ...READ MORE

answered Jul 21, 2020 in PHP by Niroj
• 82,880 points
5,881 views
0 votes
1 answer

How to specify a port to run a create-react-app based project?

Hello @kartik, You could use cross-env to set the port, ...READ MORE

answered Jul 22, 2020 in Angular by Niroj
• 82,880 points
5,322 views
0 votes
1 answer

How do you set a default value for a MySQL Datetime column?

Hello @kartik, In version 5.6.5, it is possible ...READ MORE

answered Aug 18, 2020 in PHP by Niroj
• 82,880 points
7,394 views
0 votes
1 answer

How to pass arguments to addEventListener listener function?

Try this: const someInput = document.querySelector('button'); someInput.addEventListener('click', myFunc, false); someInput.myParam ...READ MORE

answered Sep 23, 2020 in Java-Script by Niroj
• 82,880 points
10,710 views
0 votes
1 answer

How do I pass command line arguments to a Node.js program?

Hello @kartik, If your script is called myScript.js ...READ MORE

answered May 5, 2020 in Java-Script by Niroj
• 82,880 points
2,921 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP