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 trialSmith Thapa
7,023 PointsWhat is the significance of text() in the program. Isn't text() and val() performing the same task in the program??
JQuery :
https://teamtreehouse.com/library/jquery-basics/creating-a-mobile-drop-down-menu/perform-part-1
related to the video above .
2 Answers
Sergey Podgornyy
20,660 PointsI agree with Erik. But for more clearance I want to show you an example on option tag:
<select>
<option value="volvo">Volvo</option>
<option value="saab" selected>Saab</option>
<option value="opel">Opel</option>
<option value="audi">Audi</option>
</select>
Text and value will be different here:
var text = $('option:selected').text();
var val = $('option:selected').val();
alert('Text of selected option is ' + text);
alert('Value of selected option is ' + val);
You can check this on JSfiddle
Erik Nemesis
13,356 Pointstext() is for returning the text of any html element (such as p, a, span and so on), that is, displayed on the screen. But you cannot get the contents of a text field or textarea with that, instead, you need to use val()
.