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

.append .text and .attr

Can someone in the simplest form explain to me what .append, .text and .attr actually do, are, how they work? Examples would be nice also.

Thanks in advanced!!

1 Answer

Using the following HTML, I will try and help explain the jQuery functions.

<ul id="myList">
  <li data-myAttr="thing">Officia, magni.</li>
  <li>Ex, dolorum?</li>
  <li>Cumque, dolorem?</li>
  <li data-myAttr="something">Officiis, laboriosam.</li>
</ul>

Attr

The .attr() method gets the attribute value for only the first element in the matched set.

$('#myList li:first-child').attr('data-myAttr');

For instance, using the above will return 'thing', as this is the first list item and it has an attribute called data-myAttr. If the first match item doesn't have the desired attribute, it will return undefined.


Text

The .text() gets the combined text contents of each element in the set of matched elements, including their descendants, or set the text contents of the matched elements.

$('#myList').text();
$('#myLIst li:first-child').text();

For instance, using the above will return "Officia, magni. Ex, dolorum? Cumque, dolorem? Officiis, laboriosam." and "Officia, magni.".


Append

The .append() method inserts the specified content as the last child of each element in the jQuery collection.

$('#myList').append('<li>I am now last</li>');

For instance, using the above will insert the a list item with the text 'I am now last' after the last list item inside #myList.

I did, but it's confusing on the wording that they used.

Thanks for the response!

davidreyes2 I have updated my answer. Please take a look.

Thanks! This is actually helping me understand it better!

I don't quite understand this answer though....

"For instance, using the above will return 'thing', as this is the first list item and it has an attribute called data-myAttr. If the first match item doesn't have the desired attribute, it will return undefined."

Does it return the word "thing" or the words "Officia, magni." from it?

.attr() will only return a string of the value of the attribute you've selected.

For instance, $('#myList').attr('id') will return the value #myList.

So if the element you select is a list of items, only the first item will be checked for the chosen attribute.

For instance "Officia, magni."??

Only .text() returns the text inside an element. .attr() only returns the value of a given attribute.

    $('[data-myAttr=thing>').text()

will return Officia, magni.

    $('[data-myAttr=thing>').attr()

will return thing