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

William Keely
William Keely
7,295 Points

Confusion about the $select.val()

I don't understand what Andrew is talking about in the $select.val() part. Is he saying that the 'value' attribute of the 'select' element will be equal to the value attribute of the particular 'option' element?

1 Answer

That's right --the value attribute of the selected <option> is also what you send when submitting a form the old-school way.

The .val() method, when used as a getter (i.e., without passing any parameter), takes the value of the form element (input, select... ) when is triggered (here, on clicking the button). If you use a parameter, then it acts as a setter (i.e., it puts the value you are passing in the parameter as the element current value).

This method is really useful, for example, in form validation, so you can check if a form field is correct before the user actually submits the form. I think is one of my most used methods in my real life jQuery projects.