Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.


Smith Thapa
Smith Thapa
7,023 Points

What is the significance of text() in the program. Isn't text() and val() performing the same task in the program??

2 Answers

Sergey Podgornyy
Sergey Podgornyy
20,660 Points

I agree with Erik. But for more clearance I want to show you an example on option tag:

  <option value="volvo">Volvo</option>
  <option value="saab" selected>Saab</option>
  <option value="opel">Opel</option>
  <option value="audi">Audi</option>

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
Erik Nemesis
13,356 Points

text() 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().