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 Jarrin
Courses Plus Student 11,182 Pointstrouble with jQuery drop down population
I am having trouble getting my "select" menu to populate with my links
my javascript/jQuery is
var $select = $("<select></select>");
$("nav").append($select);
$select("nav a").each(function () {
var $anchor = $(this);
var $option = $("<option></option>")
$option.val($anchor.attr("href"));
$option.text($anchor.text());
$select.append($option);
});
and my HTML is
<body>
<nav>
<ul>
<li class="selected"><a href="index.html">Home</a></li>
<li><a href="about.html">About Me</a></li>
<li><a href="Portfolio.html">My Work</a></li>
<li><a href="contact.html">Contact Me</a></li>
</ul>
</nav>
<script src="http://code.jquery.com/jquery-1.11.0.min.js" type="text/javascript" charset="utf-8"></script>
<script src="jscript/mobileMenu.js" type="text/javascript" charset="utf-8"></script>
</body>
I have tried flipping my script tags at the bottom but then the "select" menu disappears all together. I also get the error message "Uncaught TypeError: object is not a function" error message on the line my anonymous function begins. Any suggestions on how to proceed? As always help is appreciated.
1 Answer
Ryan Duchene
Courses Plus Student 46,022 PointsThere's only one thing that I can see that would prevent the script from working. I believe the selector for generating the options is wrong:
var $select = '<select></select>';
$( 'nav' ).append( $select );
// Should be this
$( 'nav ul a' ).each( function () {
// your code here
});
However, there are a number of optimizations you could make to your code. For instance, changing the DOM (HTML structure) with JavaScript is very hard on the browser, and you should make an attempt to change it as little as possible. The amount of content that is added, changed, or deleted doesn't matter; it's the number of times you change it that really lags.
In your code currently, you change it once when you append the <select> element to the nav, and again for each link in the nav. That's five times total. You could reduce it and save a load on the browser by building an element gradually and then appending the whole <select> node/element in one go:
var $select = $( '<select></select>' );
var $option;
$( 'nav ul a' ).each( function() {
$option = $( '<option></option>' );
$option.val( $( this ).attr( 'href' ) );
$option.text( $( this ).text() );
$select.append( $option );
});
$( 'nav' ).append( $select );
This way, you're creating one big DOM node (or HTML element) and then adding it in one go. It saves your browser some work.
Hope this helps! Happy coding!
EDIT: Additionally, if you're not already, you should include your script tags at the bottom. I would also recommend wrapping your code in a document-ready call:
$( document ).ready( function() {
// your code
});
David Jarrin
Courses Plus Student 11,182 PointsDavid Jarrin
Courses Plus Student 11,182 PointsSo if I am reading you right you basically want to build the the options with in the selector box (all the options) before you actually append the selector box apposed to appending to the selector box, then each option within the selector box one at a time? In any case even with the changes to the old code it didn't work......but yours did, thanks a bunch!