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 trialJAKZ AIZZAT
7,813 Pointsselect or option?
Why do you use $select.val() to get the value option at the same time we set the value to the option likes this $option.val($(this).attr('href'))
2 Answers
Jacques Vincilione
17,292 PointsThe value of a "Select" (drop-down) is the currently chosen option, but to add options to the drop-down, you would add the option to the select, not just a standalone option.
<select id="mySelect">
<option value="something">Something</option> <!-- The value of the selected option is the value of the select. -->
</select>
<script type="text/javascript">
var selectedValue = $("#mySelect").val(); //this gives you the value of the select.
</script>
Hope that's not too confusing...
Adam Moore
21,956 PointsI believe that when the parentheses are blank, jQuery assumes that we are not passing in a value, so it returns one. However, when something is set inside the parentheses, the reverse is true: jQuery sees we are passing in a value for the element, and changes the value of the element.
Dave McFarland
Treehouse TeacherThis is correct. Many of jQuery's methods are both "getters" and "setters" -- that is, the method can both get a value or set a value. For example, the .html()
method either retrieves the HTML from the selection or puts HTML into the selection:
$('body').html('<h1>Hello</h1>'); // sets the HTML in the obdy of the page to <h1>Hello</h1>
alert($('body').html()); // pops up an alert with the HTML inside the body tags