Uncaught TypeError: topics.forEach is not a function
I was trying to get an output of my array.
document.addEventListener("DOMContentLoaded", function() {
document.getElementById('button').addEventListener('click', function() {
var sidebar = document.getElementById('sidebar');
var isExpanded = false;
sidebar.classList.forEach(function(className) {
if (className == 'expand') {
isExpanded = true;
}
});
if (isExpanded) {
sidebar.classList.remove('expand');
} else {
sidebar.classList.add('expand');
}
isExpanded = !isExpanded;
var contentwrapper = document.getElementById('content-wrapper');
if (isExpanded) {
contentwrapper.classList.add('padding-offset-2');
} else {
contentwrapper.classList.remove('padding-offset-2');
}
var topics = document.getElementById("content-wrapper").children;
console.log(topics);
topics.forEach(function(topic) {
console.log(topic);
})
});
I am getting the error, that topics.forEach is not a function. Can someone help me with that? I have no clue what I am doing wrong. I am trying to learn JS atm. :)
javascript
New contributor
add a comment |
I was trying to get an output of my array.
document.addEventListener("DOMContentLoaded", function() {
document.getElementById('button').addEventListener('click', function() {
var sidebar = document.getElementById('sidebar');
var isExpanded = false;
sidebar.classList.forEach(function(className) {
if (className == 'expand') {
isExpanded = true;
}
});
if (isExpanded) {
sidebar.classList.remove('expand');
} else {
sidebar.classList.add('expand');
}
isExpanded = !isExpanded;
var contentwrapper = document.getElementById('content-wrapper');
if (isExpanded) {
contentwrapper.classList.add('padding-offset-2');
} else {
contentwrapper.classList.remove('padding-offset-2');
}
var topics = document.getElementById("content-wrapper").children;
console.log(topics);
topics.forEach(function(topic) {
console.log(topic);
})
});
I am getting the error, that topics.forEach is not a function. Can someone help me with that? I have no clue what I am doing wrong. I am trying to learn JS atm. :)
javascript
New contributor
Please fix your code
– Islam Elshobokshy
yesterday
Give us the output of yourconsole.log(topics);
I assume that yourtopics
is not an array.
– holydragon
yesterday
You're trying to iterate through a list of children that don't exist. You could make the code typesafe and declaretopics && topics.forEach
but either way your application is pointless, crashed due to type error or not.
– Joseph Persie
yesterday
add a comment |
I was trying to get an output of my array.
document.addEventListener("DOMContentLoaded", function() {
document.getElementById('button').addEventListener('click', function() {
var sidebar = document.getElementById('sidebar');
var isExpanded = false;
sidebar.classList.forEach(function(className) {
if (className == 'expand') {
isExpanded = true;
}
});
if (isExpanded) {
sidebar.classList.remove('expand');
} else {
sidebar.classList.add('expand');
}
isExpanded = !isExpanded;
var contentwrapper = document.getElementById('content-wrapper');
if (isExpanded) {
contentwrapper.classList.add('padding-offset-2');
} else {
contentwrapper.classList.remove('padding-offset-2');
}
var topics = document.getElementById("content-wrapper").children;
console.log(topics);
topics.forEach(function(topic) {
console.log(topic);
})
});
I am getting the error, that topics.forEach is not a function. Can someone help me with that? I have no clue what I am doing wrong. I am trying to learn JS atm. :)
javascript
New contributor
I was trying to get an output of my array.
document.addEventListener("DOMContentLoaded", function() {
document.getElementById('button').addEventListener('click', function() {
var sidebar = document.getElementById('sidebar');
var isExpanded = false;
sidebar.classList.forEach(function(className) {
if (className == 'expand') {
isExpanded = true;
}
});
if (isExpanded) {
sidebar.classList.remove('expand');
} else {
sidebar.classList.add('expand');
}
isExpanded = !isExpanded;
var contentwrapper = document.getElementById('content-wrapper');
if (isExpanded) {
contentwrapper.classList.add('padding-offset-2');
} else {
contentwrapper.classList.remove('padding-offset-2');
}
var topics = document.getElementById("content-wrapper").children;
console.log(topics);
topics.forEach(function(topic) {
console.log(topic);
})
});
I am getting the error, that topics.forEach is not a function. Can someone help me with that? I have no clue what I am doing wrong. I am trying to learn JS atm. :)
javascript
javascript
New contributor
New contributor
New contributor
asked yesterday
KravosKravos
81
81
New contributor
New contributor
Please fix your code
– Islam Elshobokshy
yesterday
Give us the output of yourconsole.log(topics);
I assume that yourtopics
is not an array.
– holydragon
yesterday
You're trying to iterate through a list of children that don't exist. You could make the code typesafe and declaretopics && topics.forEach
but either way your application is pointless, crashed due to type error or not.
– Joseph Persie
yesterday
add a comment |
Please fix your code
– Islam Elshobokshy
yesterday
Give us the output of yourconsole.log(topics);
I assume that yourtopics
is not an array.
– holydragon
yesterday
You're trying to iterate through a list of children that don't exist. You could make the code typesafe and declaretopics && topics.forEach
but either way your application is pointless, crashed due to type error or not.
– Joseph Persie
yesterday
Please fix your code
– Islam Elshobokshy
yesterday
Please fix your code
– Islam Elshobokshy
yesterday
Give us the output of your
console.log(topics);
I assume that your topics
is not an array.– holydragon
yesterday
Give us the output of your
console.log(topics);
I assume that your topics
is not an array.– holydragon
yesterday
You're trying to iterate through a list of children that don't exist. You could make the code typesafe and declare
topics && topics.forEach
but either way your application is pointless, crashed due to type error or not.– Joseph Persie
yesterday
You're trying to iterate through a list of children that don't exist. You could make the code typesafe and declare
topics && topics.forEach
but either way your application is pointless, crashed due to type error or not.– Joseph Persie
yesterday
add a comment |
3 Answers
3
active
oldest
votes
var topics = document.getElementById("content-wrapper").children;
topics
is an HTMLCollection, not an array, and therefore does not have a forEach() method. You need to convert it to an array first.
1
Thank you very much! That solved me problem!
– Kravos
yesterday
add a comment |
var topics = document.getElementById("content-wrapper").children;
console.log(topics);
topics give collection of HTMLCollection, Convert it to array.
see this Most efficient way to convert an HTMLCollection to an Array
var topics= .slice.call(topics);
Thank you very much! That solved me problem!
– Kravos
yesterday
add a comment |
For each would not work.
try using class for the selector.
$.each( topic, function( key, value ) {
alert( key + ": " + value );
// do your stuff here
});
OR
$(".content-wrapper").each(function() {
// do your stuff here
});
Learning JS doesn't mean learning jquery :'(
– Kravos
21 hours ago
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Kravos is a new contributor. Be nice, and check out our Code of Conduct.
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%2f54251085%2funcaught-typeerror-topics-foreach-is-not-a-function%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
var topics = document.getElementById("content-wrapper").children;
topics
is an HTMLCollection, not an array, and therefore does not have a forEach() method. You need to convert it to an array first.
1
Thank you very much! That solved me problem!
– Kravos
yesterday
add a comment |
var topics = document.getElementById("content-wrapper").children;
topics
is an HTMLCollection, not an array, and therefore does not have a forEach() method. You need to convert it to an array first.
1
Thank you very much! That solved me problem!
– Kravos
yesterday
add a comment |
var topics = document.getElementById("content-wrapper").children;
topics
is an HTMLCollection, not an array, and therefore does not have a forEach() method. You need to convert it to an array first.
var topics = document.getElementById("content-wrapper").children;
topics
is an HTMLCollection, not an array, and therefore does not have a forEach() method. You need to convert it to an array first.
answered yesterday
CinnCinn
977725
977725
1
Thank you very much! That solved me problem!
– Kravos
yesterday
add a comment |
1
Thank you very much! That solved me problem!
– Kravos
yesterday
1
1
Thank you very much! That solved me problem!
– Kravos
yesterday
Thank you very much! That solved me problem!
– Kravos
yesterday
add a comment |
var topics = document.getElementById("content-wrapper").children;
console.log(topics);
topics give collection of HTMLCollection, Convert it to array.
see this Most efficient way to convert an HTMLCollection to an Array
var topics= .slice.call(topics);
Thank you very much! That solved me problem!
– Kravos
yesterday
add a comment |
var topics = document.getElementById("content-wrapper").children;
console.log(topics);
topics give collection of HTMLCollection, Convert it to array.
see this Most efficient way to convert an HTMLCollection to an Array
var topics= .slice.call(topics);
Thank you very much! That solved me problem!
– Kravos
yesterday
add a comment |
var topics = document.getElementById("content-wrapper").children;
console.log(topics);
topics give collection of HTMLCollection, Convert it to array.
see this Most efficient way to convert an HTMLCollection to an Array
var topics= .slice.call(topics);
var topics = document.getElementById("content-wrapper").children;
console.log(topics);
topics give collection of HTMLCollection, Convert it to array.
see this Most efficient way to convert an HTMLCollection to an Array
var topics= .slice.call(topics);
answered yesterday
sridhar..sridhar..
971610
971610
Thank you very much! That solved me problem!
– Kravos
yesterday
add a comment |
Thank you very much! That solved me problem!
– Kravos
yesterday
Thank you very much! That solved me problem!
– Kravos
yesterday
Thank you very much! That solved me problem!
– Kravos
yesterday
add a comment |
For each would not work.
try using class for the selector.
$.each( topic, function( key, value ) {
alert( key + ": " + value );
// do your stuff here
});
OR
$(".content-wrapper").each(function() {
// do your stuff here
});
Learning JS doesn't mean learning jquery :'(
– Kravos
21 hours ago
add a comment |
For each would not work.
try using class for the selector.
$.each( topic, function( key, value ) {
alert( key + ": " + value );
// do your stuff here
});
OR
$(".content-wrapper").each(function() {
// do your stuff here
});
Learning JS doesn't mean learning jquery :'(
– Kravos
21 hours ago
add a comment |
For each would not work.
try using class for the selector.
$.each( topic, function( key, value ) {
alert( key + ": " + value );
// do your stuff here
});
OR
$(".content-wrapper").each(function() {
// do your stuff here
});
For each would not work.
try using class for the selector.
$.each( topic, function( key, value ) {
alert( key + ": " + value );
// do your stuff here
});
OR
$(".content-wrapper").each(function() {
// do your stuff here
});
answered yesterday
AshutoshAshutosh
109111
109111
Learning JS doesn't mean learning jquery :'(
– Kravos
21 hours ago
add a comment |
Learning JS doesn't mean learning jquery :'(
– Kravos
21 hours ago
Learning JS doesn't mean learning jquery :'(
– Kravos
21 hours ago
Learning JS doesn't mean learning jquery :'(
– Kravos
21 hours ago
add a comment |
Kravos is a new contributor. Be nice, and check out our Code of Conduct.
Kravos is a new contributor. Be nice, and check out our Code of Conduct.
Kravos is a new contributor. Be nice, and check out our Code of Conduct.
Kravos is a new contributor. Be nice, and check out our Code of Conduct.
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.
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%2f54251085%2funcaught-typeerror-topics-foreach-is-not-a-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
Please fix your code
– Islam Elshobokshy
yesterday
Give us the output of your
console.log(topics);
I assume that yourtopics
is not an array.– holydragon
yesterday
You're trying to iterate through a list of children that don't exist. You could make the code typesafe and declare
topics && topics.forEach
but either way your application is pointless, crashed due to type error or not.– Joseph Persie
yesterday