Uncaught TypeError: topics.forEach is not a function












-1















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. :)










share|improve this question







New contributor




Kravos is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





















  • 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











  • 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
















-1















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. :)










share|improve this question







New contributor




Kravos is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





















  • 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











  • 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














-1












-1








-1








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. :)










share|improve this question







New contributor




Kravos is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.












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






share|improve this question







New contributor




Kravos is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question







New contributor




Kravos is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question






New contributor




Kravos is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked yesterday









KravosKravos

81




81




New contributor




Kravos is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





Kravos is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






Kravos is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.













  • 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











  • 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



















  • 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











  • 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

















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












3 Answers
3






active

oldest

votes


















1














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.






share|improve this answer



















  • 1





    Thank you very much! That solved me problem!

    – Kravos
    yesterday



















1














 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);





share|improve this answer
























  • Thank you very much! That solved me problem!

    – Kravos
    yesterday



















0














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
});





share|improve this answer
























  • Learning JS doesn't mean learning jquery :'(

    – Kravos
    21 hours ago











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.










draft saved

draft discarded


















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









1














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.






share|improve this answer



















  • 1





    Thank you very much! That solved me problem!

    – Kravos
    yesterday
















1














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.






share|improve this answer



















  • 1





    Thank you very much! That solved me problem!

    – Kravos
    yesterday














1












1








1







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.






share|improve this answer













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.







share|improve this answer












share|improve this answer



share|improve this answer










answered yesterday









CinnCinn

977725




977725








  • 1





    Thank you very much! That solved me problem!

    – Kravos
    yesterday














  • 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













1














 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);





share|improve this answer
























  • Thank you very much! That solved me problem!

    – Kravos
    yesterday
















1














 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);





share|improve this answer
























  • Thank you very much! That solved me problem!

    – Kravos
    yesterday














1












1








1







 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);





share|improve this answer













 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);






share|improve this answer












share|improve this answer



share|improve this answer










answered yesterday









sridhar..sridhar..

971610




971610













  • 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





Thank you very much! That solved me problem!

– Kravos
yesterday











0














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
});





share|improve this answer
























  • Learning JS doesn't mean learning jquery :'(

    – Kravos
    21 hours ago
















0














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
});





share|improve this answer
























  • Learning JS doesn't mean learning jquery :'(

    – Kravos
    21 hours ago














0












0








0







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
});





share|improve this answer













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
});






share|improve this answer












share|improve this answer



share|improve this answer










answered yesterday









AshutoshAshutosh

109111




109111













  • 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





Learning JS doesn't mean learning jquery :'(

– Kravos
21 hours ago










Kravos is a new contributor. Be nice, and check out our Code of Conduct.










draft saved

draft discarded


















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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

Liquibase includeAll doesn't find base path

How to use setInterval in EJS file?

Petrus Granier-Deferre