Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
david mchale
1,434 PointsSelect Box inside a function - where am i going wrong??
// A select option box from an array
var mySelectBox = function mySelectBox(){
var list = ['item1', 'item2', 'item3', 'item4'];
var box = "";
box += "<select id=\"mySelect\">";
for (var i = 0; i < list.length; i++) {
box += "<option>" + list[i] + "</option>";
}
box += "</select>";
};
function print(message){ var storeMe = document.getElementById('container'); storeMe.innerHTML = message; }
print(mySelectBox);
2 Answers
simhub
26,544 Pointshi David,
first! you have to return something in your "mySelectBox" function
var mySelectBox = function mySelectBox(){
var list = ['item1', 'item2', 'item3', 'item4'];
var box = "";
box += "<select id=\"mySelect\">";
for (var i = 0; i < list.length; i++) {
box += "<option>" + list[i] + "</option>";
}
box += "</select>";
return box; /// return something :)
};
second! you have to invoke your function in oder to give you some results
print(mySelectBox()); /// invoke by adding ()
david mchale
1,434 PointsThanks Simhub! Much appreciate that.
david mchale
1,434 Pointsdavid mchale
1,434 PointsI presumed that because the function is assigned to a variable then there is no reason to evoke the function but the variable only when calling it