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

JavaScript jQuery Basics (2014) Creating a Mobile Drop Down Menu Perform: Part 2

Udi Elenberg
Udi Elenberg
3,729 Points

There's a piece of code Im not sure the way it behave

What does this code exactly do html-wise?

var $select = $("<select></select>");
var $option = $("<option></option");
$select.append($option);

This?

<select></select>
<option></option>

Or that?

<select>
<option></option>
</select>

3 Answers

Chris Shaw
Chris Shaw
26,676 Points

Hi Udi Elenberg,

First and foremost, I've fixed your code formatting which you can read more about at Posting code to the forum.

In modern browsers, the second example is what you get back as the result, however, in some circumstances you could end up with the first example in Internet Explorer as your option tag isn't closed properly resulting in invalid HTML which modern browsers can fix on the fly sometimes.

Hope that helps.

It appends it before the closing select tag like this:

<select>
<option></option>
</select>

You can test it by copying and pasting the code into the console.

Hope this helps :)