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 trialnagamani palamuthi
6,160 PointsHi there, my code is not showing the options in the select.
cant figure out what is wrong with the code
4 Answers
Alex Hinson
20,184 PointsThe $select
only created an element, but it doesn't refer to a specific <select>
tag rendered on the page. jQuery needs to know specifically where to do something. Even though you used $select
to create the tag, it doesn't mean that $select
will always refer to that specific element it created. You could repeat your code $("#menu").append($select);
multiple times, which would create multiple <select>
tags, but the variable $select
wouldn't refer to any specific one.
Also, the syntax of the jQuery selector needs to be $(something).append();
instead of something.append();
.
Alex Hinson
20,184 PointsI'm seeing this error in the console.
app.js:22 Uncaught TypeError: $select.append is not a function(…)
One solution might be to add a class to your select:
var $select = ("<select class='main-select'></select>");
and then select it at the bottom of your function when you're trying to append the options:
$('.main-select').append($option);
nagamani palamuthi
6,160 PointsHi Alex, thnx for the help. it works now. I just added a class name for the select and selected the classname. But, I don't understand why the code did'nt run without the class name. and why it shows error ? any explanation?
Alex Hinson
20,184 PointsThe variable $select
is basically just creating a <select>
tag when you use $("#menu").append($select);
. Once the element is created on the page, the $select
variable doesn't refer to the actual <select>
element on the page.
The issue was that when you had the code $select.append($option);
the $select
variable didn't refer to any specific element on the page, so nothing was rendered.
Does that make sense?
nagamani palamuthi
6,160 Pointsbut we alresdy set thae $select variable to the <select> elment. still dont get... but how did adding a class help. after all the class is referring to the same <select> .. right?
nagamani palamuthi
6,160 Pointsnagamani palamuthi
6,160 Pointsgot it thanks!