Using a addEventListener to call a forEach function
up vote
1
down vote
favorite
So I cant find any sources online that shows an eventListener triggering a function by click that is forEach based I know i'm doing something wrong here so can any one point me to the right direction.
This is my code
document.addEventListener('DOMContentLoaded',function(){
var numbers = [4, 9, 16, 25];
document.querySelector('button').addEventListener('click',numbers.forEach(myFunction))
function myFunction(item, index) {
//create a new h1 element
var newH1 = document.createElement('h1');
//
//Insert content in the h1
var innerH1 = document.createTextNode(item);
//
//Add the text node to the newly created h1
newH1.appendChild(innerH1);
//
//Add the newly created element and its content into the DOM
document.body.insertBefore(newH1,document.body.secondChild);
//
}
});
<button>Click me</button>
javascript
add a comment |
up vote
1
down vote
favorite
So I cant find any sources online that shows an eventListener triggering a function by click that is forEach based I know i'm doing something wrong here so can any one point me to the right direction.
This is my code
document.addEventListener('DOMContentLoaded',function(){
var numbers = [4, 9, 16, 25];
document.querySelector('button').addEventListener('click',numbers.forEach(myFunction))
function myFunction(item, index) {
//create a new h1 element
var newH1 = document.createElement('h1');
//
//Insert content in the h1
var innerH1 = document.createTextNode(item);
//
//Add the text node to the newly created h1
newH1.appendChild(innerH1);
//
//Add the newly created element and its content into the DOM
document.body.insertBefore(newH1,document.body.secondChild);
//
}
});
<button>Click me</button>
javascript
Add})
to end of code
– Mohammad
Nov 22 at 9:43
What is your expected end result
– Nedko Dimitrov
Nov 22 at 9:43
secondChild
isn’t a real thing. The rest looks OK
– Ben West
Nov 22 at 9:46
The problem is, thataddEventListener
expects a function reference/expression as the second argument,forEach
returnsundefined
, hence no action is executed on a button click. Notice, that the iteration is executed immediately at the time you're attaching the listener.
– Teemu
Nov 22 at 9:50
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
So I cant find any sources online that shows an eventListener triggering a function by click that is forEach based I know i'm doing something wrong here so can any one point me to the right direction.
This is my code
document.addEventListener('DOMContentLoaded',function(){
var numbers = [4, 9, 16, 25];
document.querySelector('button').addEventListener('click',numbers.forEach(myFunction))
function myFunction(item, index) {
//create a new h1 element
var newH1 = document.createElement('h1');
//
//Insert content in the h1
var innerH1 = document.createTextNode(item);
//
//Add the text node to the newly created h1
newH1.appendChild(innerH1);
//
//Add the newly created element and its content into the DOM
document.body.insertBefore(newH1,document.body.secondChild);
//
}
});
<button>Click me</button>
javascript
So I cant find any sources online that shows an eventListener triggering a function by click that is forEach based I know i'm doing something wrong here so can any one point me to the right direction.
This is my code
document.addEventListener('DOMContentLoaded',function(){
var numbers = [4, 9, 16, 25];
document.querySelector('button').addEventListener('click',numbers.forEach(myFunction))
function myFunction(item, index) {
//create a new h1 element
var newH1 = document.createElement('h1');
//
//Insert content in the h1
var innerH1 = document.createTextNode(item);
//
//Add the text node to the newly created h1
newH1.appendChild(innerH1);
//
//Add the newly created element and its content into the DOM
document.body.insertBefore(newH1,document.body.secondChild);
//
}
});
<button>Click me</button>
document.addEventListener('DOMContentLoaded',function(){
var numbers = [4, 9, 16, 25];
document.querySelector('button').addEventListener('click',numbers.forEach(myFunction))
function myFunction(item, index) {
//create a new h1 element
var newH1 = document.createElement('h1');
//
//Insert content in the h1
var innerH1 = document.createTextNode(item);
//
//Add the text node to the newly created h1
newH1.appendChild(innerH1);
//
//Add the newly created element and its content into the DOM
document.body.insertBefore(newH1,document.body.secondChild);
//
}
});
<button>Click me</button>
document.addEventListener('DOMContentLoaded',function(){
var numbers = [4, 9, 16, 25];
document.querySelector('button').addEventListener('click',numbers.forEach(myFunction))
function myFunction(item, index) {
//create a new h1 element
var newH1 = document.createElement('h1');
//
//Insert content in the h1
var innerH1 = document.createTextNode(item);
//
//Add the text node to the newly created h1
newH1.appendChild(innerH1);
//
//Add the newly created element and its content into the DOM
document.body.insertBefore(newH1,document.body.secondChild);
//
}
});
<button>Click me</button>
javascript
javascript
edited Nov 22 at 9:44
asked Nov 22 at 9:39
James Dean
604
604
Add})
to end of code
– Mohammad
Nov 22 at 9:43
What is your expected end result
– Nedko Dimitrov
Nov 22 at 9:43
secondChild
isn’t a real thing. The rest looks OK
– Ben West
Nov 22 at 9:46
The problem is, thataddEventListener
expects a function reference/expression as the second argument,forEach
returnsundefined
, hence no action is executed on a button click. Notice, that the iteration is executed immediately at the time you're attaching the listener.
– Teemu
Nov 22 at 9:50
add a comment |
Add})
to end of code
– Mohammad
Nov 22 at 9:43
What is your expected end result
– Nedko Dimitrov
Nov 22 at 9:43
secondChild
isn’t a real thing. The rest looks OK
– Ben West
Nov 22 at 9:46
The problem is, thataddEventListener
expects a function reference/expression as the second argument,forEach
returnsundefined
, hence no action is executed on a button click. Notice, that the iteration is executed immediately at the time you're attaching the listener.
– Teemu
Nov 22 at 9:50
Add
})
to end of code– Mohammad
Nov 22 at 9:43
Add
})
to end of code– Mohammad
Nov 22 at 9:43
What is your expected end result
– Nedko Dimitrov
Nov 22 at 9:43
What is your expected end result
– Nedko Dimitrov
Nov 22 at 9:43
secondChild
isn’t a real thing. The rest looks OK– Ben West
Nov 22 at 9:46
secondChild
isn’t a real thing. The rest looks OK– Ben West
Nov 22 at 9:46
The problem is, that
addEventListener
expects a function reference/expression as the second argument, forEach
returns undefined
, hence no action is executed on a button click. Notice, that the iteration is executed immediately at the time you're attaching the listener.– Teemu
Nov 22 at 9:50
The problem is, that
addEventListener
expects a function reference/expression as the second argument, forEach
returns undefined
, hence no action is executed on a button click. Notice, that the iteration is executed immediately at the time you're attaching the listener.– Teemu
Nov 22 at 9:50
add a comment |
1 Answer
1
active
oldest
votes
up vote
2
down vote
accepted
You could try to put numbers.forEach(myFunction);
to a function inside the event, like this:
document.addEventListener('DOMContentLoaded', function () {
var numbers = [4, 9, 16, 25];
document.querySelector('button').addEventListener('click', function () {
numbers.forEach(myFunction);
});
function myFunction(item, index) {
//create a new h1 element
var newH1 = document.createElement('h1');
//
//Insert content in the h1
var innerH1 = document.createTextNode(item);
//
//Add the text node to the newly created h1
newH1.appendChild(innerH1);
//
//Add the newly created element and its content into the DOM
document.body.insertBefore(newH1, document.body.secondChild);
//
}
});
<button>Click me</button>
Syntax:
selector.addEventListener(event, function () {
// implement...
});
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
You could try to put numbers.forEach(myFunction);
to a function inside the event, like this:
document.addEventListener('DOMContentLoaded', function () {
var numbers = [4, 9, 16, 25];
document.querySelector('button').addEventListener('click', function () {
numbers.forEach(myFunction);
});
function myFunction(item, index) {
//create a new h1 element
var newH1 = document.createElement('h1');
//
//Insert content in the h1
var innerH1 = document.createTextNode(item);
//
//Add the text node to the newly created h1
newH1.appendChild(innerH1);
//
//Add the newly created element and its content into the DOM
document.body.insertBefore(newH1, document.body.secondChild);
//
}
});
<button>Click me</button>
Syntax:
selector.addEventListener(event, function () {
// implement...
});
add a comment |
up vote
2
down vote
accepted
You could try to put numbers.forEach(myFunction);
to a function inside the event, like this:
document.addEventListener('DOMContentLoaded', function () {
var numbers = [4, 9, 16, 25];
document.querySelector('button').addEventListener('click', function () {
numbers.forEach(myFunction);
});
function myFunction(item, index) {
//create a new h1 element
var newH1 = document.createElement('h1');
//
//Insert content in the h1
var innerH1 = document.createTextNode(item);
//
//Add the text node to the newly created h1
newH1.appendChild(innerH1);
//
//Add the newly created element and its content into the DOM
document.body.insertBefore(newH1, document.body.secondChild);
//
}
});
<button>Click me</button>
Syntax:
selector.addEventListener(event, function () {
// implement...
});
add a comment |
up vote
2
down vote
accepted
up vote
2
down vote
accepted
You could try to put numbers.forEach(myFunction);
to a function inside the event, like this:
document.addEventListener('DOMContentLoaded', function () {
var numbers = [4, 9, 16, 25];
document.querySelector('button').addEventListener('click', function () {
numbers.forEach(myFunction);
});
function myFunction(item, index) {
//create a new h1 element
var newH1 = document.createElement('h1');
//
//Insert content in the h1
var innerH1 = document.createTextNode(item);
//
//Add the text node to the newly created h1
newH1.appendChild(innerH1);
//
//Add the newly created element and its content into the DOM
document.body.insertBefore(newH1, document.body.secondChild);
//
}
});
<button>Click me</button>
Syntax:
selector.addEventListener(event, function () {
// implement...
});
You could try to put numbers.forEach(myFunction);
to a function inside the event, like this:
document.addEventListener('DOMContentLoaded', function () {
var numbers = [4, 9, 16, 25];
document.querySelector('button').addEventListener('click', function () {
numbers.forEach(myFunction);
});
function myFunction(item, index) {
//create a new h1 element
var newH1 = document.createElement('h1');
//
//Insert content in the h1
var innerH1 = document.createTextNode(item);
//
//Add the text node to the newly created h1
newH1.appendChild(innerH1);
//
//Add the newly created element and its content into the DOM
document.body.insertBefore(newH1, document.body.secondChild);
//
}
});
<button>Click me</button>
Syntax:
selector.addEventListener(event, function () {
// implement...
});
document.addEventListener('DOMContentLoaded', function () {
var numbers = [4, 9, 16, 25];
document.querySelector('button').addEventListener('click', function () {
numbers.forEach(myFunction);
});
function myFunction(item, index) {
//create a new h1 element
var newH1 = document.createElement('h1');
//
//Insert content in the h1
var innerH1 = document.createTextNode(item);
//
//Add the text node to the newly created h1
newH1.appendChild(innerH1);
//
//Add the newly created element and its content into the DOM
document.body.insertBefore(newH1, document.body.secondChild);
//
}
});
<button>Click me</button>
document.addEventListener('DOMContentLoaded', function () {
var numbers = [4, 9, 16, 25];
document.querySelector('button').addEventListener('click', function () {
numbers.forEach(myFunction);
});
function myFunction(item, index) {
//create a new h1 element
var newH1 = document.createElement('h1');
//
//Insert content in the h1
var innerH1 = document.createTextNode(item);
//
//Add the text node to the newly created h1
newH1.appendChild(innerH1);
//
//Add the newly created element and its content into the DOM
document.body.insertBefore(newH1, document.body.secondChild);
//
}
});
<button>Click me</button>
answered Nov 22 at 9:43
Foo
1
1
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53427865%2fusing-a-addeventlistener-to-call-a-foreach-function%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Add
})
to end of code– Mohammad
Nov 22 at 9:43
What is your expected end result
– Nedko Dimitrov
Nov 22 at 9:43
secondChild
isn’t a real thing. The rest looks OK– Ben West
Nov 22 at 9:46
The problem is, that
addEventListener
expects a function reference/expression as the second argument,forEach
returnsundefined
, hence no action is executed on a button click. Notice, that the iteration is executed immediately at the time you're attaching the listener.– Teemu
Nov 22 at 9:50