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


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